Tuesday, June 7, 2011

PowerShell script to gather various information on Domain Controllers

I recently got tasked with upgrading all of my Windows 2003 domain controllers to Windows 2008 R2. Now, since I have domain controllers all over the world and IT staff in each country who will be performing the work for their particulare divisions, I needed a quick and easy way to see which servers were on which OS and what site they belonged to and I needed it on demand so I could keep up with the project as it moved forward. PowerShell made it easy for me.

The following script will create an output file, give it a tab delimited header, and then populate it with various bits of information for all available domain controllers in the domain. By available, I mean the server needs to be up and online at the time the script is run. To gather for more than one domain, you will need to run this on a domain controller in each domain.

This script will need to be run in the Active Directory Module for Windows Powershell.
#first, let's create the output file
$out = "c:\Output.txt"
out-file $out -inputobject "Name`tOS`tSite`tFSMO Roles`tLDAP Port`tGlobal Catalog"
#now, let's get a list of all Domain Controllers
$dcs = get-addomaincontroller -filter { name -like "*" }
#next, you want to iterate through each domain controller and capture it's information.
foreach ($dc in $dcs)
#get the operating system of the current DC
   $OS = $dc.OperatingSystem
#get the name of the current DC
   $name = $dc.Name
#get the AD site the DC is in
   $site = $dc.Site
#get the FSMO roles of the DC (if any)
   $FSMO = $dc.OperationMasterRoles
#get the LDAP port the DC listens on
   $ldap = $dc.LdapPort
#determine whether the DC is a global catalog server or not
   $gcq = $dc.IsGlobalCatalog
   if ($gcq -eq $true) 
      $gc = "Yes"
      $gc = "No"
#write out all of the results to a single, tab delimited line
   $inp = "$name`t$os`t$site`t$FSMO`t$ldap`t$gc"
#write the tab delimited line to the output file
   out-file $out -inputobject $inp -append

That's it. Pretty cut and dry. You can open the output.txt file in Excel since it is tab delimited and it will line up nice and neat in the proper columns.

No comments:

Post a Comment