$hostEntry=New-Object -TypeName System.Net.IPHostEntry
$configurationContainer = ([adsi] "LDAP://RootDSE").Get("ConfigurationNamingContext")
$partitions = ([adsi] "LDAP://CN=Partitions,$configurationContainer").psbase.children
"Domain`tDomainController`tIPAddress" | Out-File -FilePath "DomainControllers.txt"
foreach($partition in $partitions)
{
if($partition.netbiosName -ne ""){
"DCs in the " + $partition.netbiosName + " Domain"
$partitionDN=$partition.ncName
$dcContainer=[adsi] "LDAP://ou=domain controllers,$partitionDN"
$dcs = $dcContainer.psbase.children
foreach($dc in $dcs){
$hostEntry= [System.Net.Dns]::GetHostByName($dc.dnsHostName)
"`t" + $dc.dnsHostName + "`t" + $hostEntry.AddressList[0].IPAddressToString
"$($partition.netbiosName)`t$($dc.dnsHostName)`t$($hostEntry.AddressList[0].IPAddressToString)" | Out-File -FilePath "DomainControllers.txt" -Append
}
}
}Related Posts:
- Backup DFS Namespaces Using PowerShell
- Translate Active Directory Name Formats Using PowerShell
- List Linux Users in Active Directory Using PowerShell
- Enable Trust for Delegation in Active Directory Using PowerShell
- TCP/IP Subnet Math with PowerShell - What AD Site is that Server in?
- List Sites and Subnets in Active Directory with PowerShell
- Find Disabled Users in Active Directory with PowerShell
- List Forest-wide Group Memberships with PowerShell
- Find Old Computer Accounts in AD with PowerShell
- List SPNs in Active Directory with PowerShell
- List Domain Controllers in Active Directory

Very nice.
ReplyDeletethanks
If copying and pasting, need to replace ¦ character with | (twice)
ReplyDeleteFixed. Thanks for catching that!
ReplyDelete