Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

Get Active Directory Forest Domain Controllers

Posted on January 25, 2020 by Alan

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

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy