Like the other script posted today, this was written to help out the networking team with a simple inventory of DCs in a large Active Directory forest. It queries all domains in the forest, creating a CSV file with the Domain, DC name, IP Address, OS, AD Site and Roles. It can optionally ping the DCs to see whether they are online. Some code notes:
- Line 9 uses [environment]::GetFolderPath(‘Desktop’) instead of the more familiar $env:userprofile\Desktop. Unlike the $Env drive, this method works when there is GPO based desktop re-direction.
- Lines 17-20 demonstrate that for a menu list of multiple lines you can simply uses quotes at the beginning and end
- Lines 29 through 54 are wrapped as an array, beginning with “@(” and ending with “)”. This is a simple workaround to allow you to pipe the entire results to Export-csv
- If you uncomment line 65, you will see how using invoke-item will open the log file in Excel or other program associated with .CSV files.
<# Get-ForestDClist.ps1 Alan Kaplan 1/24/20 Get list of DCs in Forest, with optional ping Does not rely on AD module or admin rights #> #Default logfile $desktop = [environment]::GetFolderPath('Desktop') $logfile = "$Desktop\DCs_" + $(Get-Date).ToString("yyyyMMdd_HHmm") + '.csv' $Forest = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()) $RootDom = ($Forest).RootDomain $dList = ($Forest.Domains).Name | Sort-Object -CaseSensitive Add-Type -assemblyname Microsoft.visualBasic $msg = "This will get a list of all DCs in $RootDom. 1) Get list with online state (ping) 2) Get list without online state (no ping) 0) Exit" $retval = [Microsoft.VisualBasic.Interaction]::InputBox($msg, "Create List of All DCs in Forest", 0) switch ($retval) { 1 { $bPing = $true } 2 { $bPing = $false } Default { Exit } } Write-Host "Getting all DCs in $rootDom ..." @(foreach ($dom in $dlist) { $DomainContext = new-object System.directoryServices.ActiveDirectory.DirectoryContext("Domain", $Dom) $ADSIDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($DomainContext) $ADSIDomain.DomainControllers | ForEach-Object { $msg = "Getting all DCs in $dom" if ($bPing) { $msg = "$msg with online state" } Write-Progress -activity $msg -status ($_).Name if ($bPing) { $bOnline = Test-Connection $_.IPAddress -count 1 -Quiet -ea SilentlyContinue } Else { $bOnline = "Not Tested" } [PSCustomObject]@{ Domain = $domc Name = $_.Name 'IP Address' = $_.IPAddress Online = $bOnline OS = $_.OSVersion 'AD Site' = $_.SiteName 'AD Roles' = ($_.Roles -join ", ").toUpper() -replace 'Role', '' } } }) | Sort-Object Domain, Name | Export-csv $logfile -NoTypeInformation Write-Progress "Done" -completed Clear-Host Write-host "Done. Report is $logfile" #Open report when done #invoke-item $logfile