Partager via


PowerShelling your DC's

The following is useful for scenarios where you want to either batch process a command online against all of the DC's in the domain or if you have data files from all the DC's that you want to process offline.

The PS script will check for the presence of a file called dclist.csv with the output of all DC's in the domain (or export the same to that file if not present) and do a loop of DSQuery to do a simple check for the LDAP response time for each.  You'll need PowerShell 3.0 on the box you're running this from and you also need to add the AD PowerShell module option in the Remote Server Administration Tools section (Add Features Wizard on the server variant or in Programs on the client versions).

 clear-host
 $objsum = New-Object -TypeName PSObject
 $chkfile = test-path DCList.csv
 $ScriptStart = Get-Date
 [string[]]$DCInfo=""
 $Msg= @()
 
 function getExTime ([object]$variable)
 {
 " ...completed in $($variable.Days) days, $($variable.Hours) hours, $($variable.Minutes) minutes, $($variable.Seconds) seconds, $($variable.Milliseconds) milliseconds"
 }
 
 "Processing DC's:"
 
 if ($chkfile -eq "True") {
 "Found DCList.csv - loading it"
 $DCList=import-csv DCList.csv|select-object
 "...imported list of DC's from DCList.csv"
 }
 else {
 Write-host "No DCList.csv found - creating it"
 import-module ActiveDirectory
 "...imported AD PowerShell module"
 $DCList=get-addomaincontroller -filter *
 $DCList|Export-CSV DCList.csv
 }
 
 "`nBreakdown of DC's in domain:"
 $DCList | ForEach {
 $DCInfo+="$($_.OperatingSystem) $($_.OperatingSystemServicePack)"|sort-object -unique
 }
 $DCsInDomain=($DCinfo.Count)-1
 $tableFormat = @{Expression={$_.Count};Label="Count";width=6}, @{Expression={$_.Name};Label="Operating System and Service Pack of DC's";width=120}
 $msg=$DCInfo|select-string -pattern "Server"|%{$data =[regex]::Replace($_,'(Windows Server).+ 2008', 'Windows Server 2008'); Write-Output "$($data)"}|Group-Object |Sort-Object -Descending -Property Count| Select-Object Count,Name |ft $tableFormat -autosize
 $msg
 "`nTotal number of DC's in domain: $($DCsInDomain)"
 
 
 $DCList | ForEach {
 #Do some tests
 "`nQuerying $($_.name) for partitions using LDAP:"
 $xScripts=Measure-command {
 dsquery partition -server $_.name
 }
 getExTime($xScripts)
 Add-Member -inputobject $objsum -membertype NoteProperty -name "$($_.name)" -value "$($xScripts)"
 
 }
 
 $objsum|sort-object|fl