I know this is an old tread but I wanted to share some code I put together for a single domain lookup and a multi-domain a one-liner to find the closest DC from any windows device running PowerShell 3+. I use this to help balance Sites and Services.
This block will search a single domain for all DC's and the number of hops and latency to each DC
$domain="<DOMAIN FQDN>";$ip=(Get-NetIPAddress -InterfaceIndex (Get-NetRoute -DestinationPrefix "0.0.0.0/0").ifIndex);$results=@();$a=New-Object 'System.DirectoryServices.ActiveDirectory.DirectoryContext'("domain",$domain);[System.DirectoryServices.ActiveDirectory.DomainController]::FindAll($a)|%{ $temp=Test-NetConnection $_.name -TraceRoute; $results+=[pscustomobject]@{SourceIP="$($ip.IPAddress)/$($ip.PrefixLength)";LogonServer=$env:LOGONSERVER; DomainController=$_.name;IP=$_.IPaddress;Site=$_.sitename;OS=$_.OSVersion;ResponseTime=$temp.PingReplyDetails.RoundtripTime;HopCount=($temp.TraceRoute).count}}; $results | sort ResponseTime | Out-GridView -Title "DC Finder"`
Here's a multi-domain expanded version of the domain controller status script.
# define your domain(s)
$domains="<FQDN DOMAIN 1>","<FQDN DOMAIN 2>"
# example domain list. uncomment the below line and add in all the domains.
# $domains="corp.domain.com","subdomain.corp.domain.com"
# get the IP information of the local computer running this code block
$ip=(Get-NetIPAddress -InterfaceIndex (Get-NetRoute -DestinationPrefix "0.0.0.0/0").ifIndex)
# create an empty arraylist object
$results = New-Object System.Collections.ArrayList
$domains | ForEach-Object {
$domain = ($_).toupper()
$a=New-Object 'System.DirectoryServices.ActiveDirectory.DirectoryContext'("domain",$domain)
[System.DirectoryServices.ActiveDirectory.DomainController]::FindAll($a) | `
ForEach-Object {
$ping = Test-NetConnection $_.name -TraceRoute
$tmpobj = New-Object -TypeName PSObject -Property ([ordered]@{
domainFQDN = $domain
sourceIP = "$($ip.IPAddress)/$($ip.PrefixLength)"
logonServer = $env:LOGONSERVER
domainController = $_.name
domainControllerIP = $_.IPaddress
siteName = $_.sitename
domainControllerOS = $_.OSVersion
responseTime=$ping.PingReplyDetails.RoundtripTime;
HopCount=($ping.TraceRoute).count
})
}
$null = $results.add($tmpobj)
}
$results | sort responseTime | Out-GridView -Title "Domain Controller Stats"
After the script completes it will pop up a PowerShell Gridview window with the results.