Monday, July 11, 2016

Finding all DNS servers in the domain

Learning a new environment is tough sometimes, not wanting to be the new guy asking all the questions.  One of the things I wrote to help me gather information without asking or giving me enough information to ask an intelligent question was the script below.  It was written specifically to find servers running the DNS service but can be modified easily to find any service running on windows servers in your domain.

Originally I wrote the script like this:

Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -Property *  | foreach { Get-Service -name "DNS" -ComputerName $_.Name -ErrorAction SilentlyContinue | Select-Object -Property MachineName,ServiceName,Status }

But kept erroring out. Exact error:

Get-ADComputer : The server has returned the following error: invalid enumeration context. 

I googled some and found this: 


So I followed the script modification recommendations and ended up with this:

$adobjects = Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -Property *; $adobjects = | foreach { Get-Service -name "DNS" -ComputerName $_.Name -ErrorAction SilentlyContinue | Select-Object -Property MachineName,ServiceName,Status } 

The script ran to completion without error.  If you want to export to CSV add this to the end of the script.

| Export-CSV "C:\temp\DNS_Servers.csv" -NoClobber -NoTypeInformation


Just so you know, depending on the number of server in your domain this script can take a while to complete, but it does complete.

No comments:

Post a Comment