Sometimes it is necessary to query a Global Catalog (port 3268) for forest-wide data (such as checking for a ServicePrincipalName). Finding a local online GC is the best method for this. The script method prefers 2008 DCs.
Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | Import-Module ActiveDirectory IF ($TargetGC) { ## OPEN IF TargetGC has a value $GCInfo = Get-ADDomainController $TargetGC IF ($GCInfo.OperatingSystemVersion -lt 6.0) { ## OPEN IF TargetGC is not running Windows 2008 or higher $LocalSite = (Get-ADDomainController -Discover).Site $NewTargetGC = Get-ADDomainController -Discover -Service 6 -SiteName $LocalSite IF (!$NewTargetGC) { $NewTargetGC = Get-ADDomainController -Discover -Service 6 -NextClosestSite } $LocalGC = $NewTargetGC.HostName + ":3268" } ## CLOSE IF TargetGC is not running Windows 2008 or higher ELSE { $LocalGC = $GCInfo.HostName + ":3268" } } ## CLOSE IF TargetGC has a value ELSE { ## OPEN ELSE TargetGC is not set Write-Output "Discover Local GC running ADWS `r " $LocalSite = (Get-ADDomainController -Discover).Site $NewTargetGC = Get-ADDomainController -Discover -Service 6 -SiteName $LocalSite IF (!$NewTargetGC) { $NewTargetGC = Get-ADDomainController -Discover -Service 6 -NextClosestSite } $LocalGC = $NewTargetGC.HostName + ":3268" } ## CLOSE ELSE TargetGC is not set |