List All Domain Controllers and roles with PowerShell

New job, new infrastructure, and I needed a way to quickly find out what Active Directory Domain Controllers were present, where they were located, and what roles and functions they all performed.
So I wrote this simple script to do it.

$DCs = Get-ADDomainController -Filter *
$Results = New-Object -TypeName System.Collections.ArrayList
foreach($DC in $DCs){
    [string]$OMRoles = ""
    $ThisResult = New-Object -TypeName System.Object
    Add-Member -InputObject $ThisResult -MemberType NoteProperty -Name Name -Value $DC.Name
    Add-Member -InputObject $ThisResult -MemberType NoteProperty -Name Site -Value $DC.Site
    Add-Member -InputObject $ThisResult -MemberType NoteProperty -Name IPv4Address -Value $DC.IPv4Address
    Add-Member -InputObject $ThisResult -MemberType NoteProperty -Name OperatingSystemVersion -Value $DC.OperatingSystemVersion
    Add-Member -InputObject $ThisResult -MemberType NoteProperty -Name IsGlobalCatalog -Value $DC.IsGlobalCatalog
    Add-Member -InputObject $ThisResult -MemberType NoteProperty -Name IsReadOnly -Value $DC.IsReadOnly
    foreach($OMRole in $DC.OperationMasterRoles){
        $OMRoles += ([string]$OMRole+" ")
    Add-Member -InputObject $ThisResult -MemberType NoteProperty -Name OperationMasterRoles -Value $OMRoles
    $Results.Add($ThisResult) | Out-Null
$Results = $Results | Sort-Object -Property Site
$Results | Format-Table -AutoSize

You could also export the $Results object to CSV via Export-CSV.

This entry was posted in PowerShell and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s