We can list all the Active Directory Domain Controllers in current domain or forest using .NET classes System.Directoryservices.Activedirectory.Domain and System.Directoryservices.Activedirectory.Forest. We can resolve IP Address for every Domain Controller by using .NET class System.Net.Dns.
Note: This PowerShell script doesn’t require to import Active Directory Module since it is using the methods from .NET Framework.
List all Domain Controllers and IP Address in Current Domain
This following script returns all the Domain Controllers from current Domain and resolves IP address by DNS lookup. The results are output to the PowerShell console screen.
$domain = [System.Directoryservices.Activedirectory.Domain]::GetCurrentDomain() $domain | ForEach-Object {$_.DomainControllers} | ForEach-Object { $hostEntry= [System.Net.Dns]::GetHostByName($_.Name) New-Object -TypeName PSObject -Property @{ Name = $_.Name IPAddress = $hostEntry.AddressList[0].IPAddressToString } } | Select Name, IPAddress
Export List of Domain Controllers and IP Address to CSV
We can export PowerShell output into CSV file using Export-CSV cmdlet. The following PowerShell script find all the Domain Controllers from current Domain and resolve IP address by DNS lookup and exports output to CSV file.
$domain = [System.Directoryservices.Activedirectory.Domain]::GetCurrentDomain() $domain | ForEach-Object {$_.DomainControllers} | ForEach-Object { $hostEntry= [System.Net.Dns]::GetHostByName($_.Name) New-Object -TypeName PSObject -Property @{ Name = $_.Name IPAddress = $hostEntry.AddressList[0].IPAddressToString } } | Export-CSV "C:\DomainControllers.csv" -NoTypeInformation -Encoding UTF8
Advertisement
Thanks Morgan,
You save my day