Showing posts with label Get-ADComputer. Show all posts
Showing posts with label Get-ADComputer. Show all posts

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.

Sunday, July 3, 2016

Document all Windows Server in the Domain

At my last job my CIO would always call me asking “what does server x do?” when change control voting was taking place.  This was mostly because our naming convention in no way resembled what the purpose of each server was.  So I decided to create a CSV weekly via scheduled task that documented the name of each server, its OS, and IP.  But most importantly it pulled in the description field from the computer object.  I know some of you will have to go back through your server objects and add a description but this was well worth the time and it made my CIO feel like I did this to make their job easier.

I used the Get-ADComputer but I had to make sure I only pulled out server OS.  So, I had to add a filter based off of the OperatingSystem field. Example: {OperatingSystem -Like "Windows *Server*"}  So if you wanted to find all Windows XP machines, just change the filter. Example: {OperatingSystem -Like "Windows *XP*"}  But alternatively if you want to find all machines expect Windows XP just change the filter to {OperatingSystem -NotLike "Windows *XP*"}

Start of script

Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -Property * | Select-Object -Property Name,Description,OperatingSystem,OperatingSystemServicePack,IPv4Address | Export-Csv '\\your share\Server_Inventory.csv' -NoTypeInformation -Encoding UTF8

End of script


You can also use the Send-MailMessage command to email the csv as an attachment.  The command can be found here: http://mytechnicalsolution.blogspot.com/search/label/send-MailMessage