Skip to content

Instantly share code, notes, and snippets.

@kspeeckaert
Last active September 16, 2021 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kspeeckaert/c9953ada44b51630a1aeb41d09bf4eee to your computer and use it in GitHub Desktop.
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).
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
@kspeeckaert
Copy link
Author

Sample output:

ssesvgkm1191.acme.site......[00:00:00.0412119]
IIFIKWHT1101.acme.site......[00:00:00.0462304]
IIFIKWHT1171.acme.site......[00:00:00.0685567]
IIFIKWHT1161.acme.site......[00:00:00.0768323]
IIFIKWHT1110.acme.site......[00:00:00.1871499]
ssesvgkm1117.acme.site......[00:00:00.1922456]
ssesvgkm1119.acme.site......[00:00:00.1952457]
RFYIKWHT1117.acme.site......[00:00:00.1962261]
RFYIKWHT1119.acme.site......[00:00:00.2055862]
WPCIHWHT1116.acme.site......[00:00:00.2077982]
IITINGHT117.acme.site.......[00:00:00.2783724]
IITIKWRHTS119.acme.site.....[00:00:00.3024234]
CYWIZYHT19.acme.site........[00:00:00.3689528]
IIFITAHT1119.acme.site......[00:00:00.3692130]
IIFIFLHT1113.acme.site......[00:00:00.3983722]
ENRIENHT9119.acme.site......[00:00:00.4341225]
ssmssgkm1110.acme.site......[00:00:00.4353417]
IIFISMHT16.acme.site........[00:00:00.4454068]
WIZIWIHT1119.acme.site......[00:00:00.6379755]
IIFIYNHT19.acme.site........[00:00:05.5756056]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment