Last active
September 16, 2021 10:21
-
-
Save kspeeckaert/c9953ada44b51630a1aeb41d09bf4eee to your computer and use it in GitHub Desktop.
Given a username and a domain, perform a lookup on each DC and measure the time it takes (if successful).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-ADUser() { | |
[CmdletBinding()] | |
<# | |
Perform a user account lookup on a given DC and return a boolean | |
indicating success or failure. | |
#> | |
param( | |
[string]$UserName, | |
[string]$Server | |
) | |
try { | |
Get-ADUser -Identity $UserName -Server $Server | Out-Null | |
return $true | |
} catch { | |
return $false | |
} | |
} | |
function Test-DCResponse() { | |
<# | |
Find the domain controllers for a given domain, iterate through each one, | |
perform a user lookup, measure how much time it took and if it was successful. | |
#> | |
param( | |
[string]$UserName, | |
[string]$Domain | |
) | |
$Servers = Get-ADDomainController -Server $Domain -Filter * | Select-Object -ExpandProperty 'HostName' | Sort-Object | |
$Results = @() | |
$i=0 | |
foreach ($Server in $Servers) { | |
$i+=1 | |
Write-Progress -Activity 'Testing DC response time...' -Status $Server -PercentComplete ($i / $Servers.count * 100 ) | |
$Duration = Measure-Command { | |
Test-ADUser -UserName $UserName -Server $Server -OutVariable LookupSuccess | |
} | |
$Results += [PSCustomObject] @{ | |
'HostName' = $Server | |
'Success' = $LookupSuccess[0] | |
'Duration' = $Duration | |
} | |
} | |
return $Results | |
} | |
function Format-Results() { | |
<# | |
Output the timing results to stdout, use color to indicate success or failure. | |
#> | |
param( | |
[PSCustomObject[]]$Results | |
) | |
$OutputPadding = ($Results | Select-Object -ExpandProperty HostName | Measure-Object -Maximum -Property Length).Maximum + 5 | |
foreach ($Result in $Results) { | |
if ($Result.Success) { | |
$OutputColor = 'green' | |
} else { | |
$OutputColor = 'red' | |
} | |
Write-Host $Result.HostName.PadRight($OutputPadding, '.') -NoNewline | |
Write-Host "[$($Result.Duration.ToString())]" -ForegroundColor $OutputColor | |
} | |
} | |
$Results = Test-DCResponse -UserName 'wile.e.coyote' -Domain 'acme.site' | Sort-Object -Property Duration | |
Format-Results -Results $Results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: