Skip to content

Instantly share code, notes, and snippets.

@bill-long
Last active October 29, 2020 18:28
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 bill-long/97af765087ada1d0d46cfa12b479328d to your computer and use it in GitHub Desktop.
Save bill-long/97af765087ada1d0d46cfa12b479328d to your computer and use it in GitHub Desktop.
$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$gc = $forest.FindGlobalCatalog()
$searcher = $gc.GetDirectorySearcher()
$searcher.Filter = "(&(objectClass=user)(objectSID=*))"
# Since we're not setting a page size, this will return 1000 items and then stop
$objects = $searcher.FindAll()
$sidList = @()
foreach ($o in $objects) {
$sid = New-Object System.Security.Principal.SecurityIdentifier(($o.Properties["objectSid"][0]), 0)
$sidList += $sid
}
Write-Host "Found $($sidList.Count) SIDs. Starting resolve loop. Ctrl-C to stop."
# We have 1000 sids. Look them up in a loop, and log any errors.
$count = 0
$errorCount = 0
while ($true) {
foreach ($sid in $sidList) {
try {
$count += 1
Write-Progress -Activity "Resolving $sid" -Status "$count total / $errorCount errors"
$sid.Translate("System.Security.Principal.NTAccount") | Out-Null
} catch {
$errorCount += 1
Write-Error $_
Write-EventLog -LogName "Application" -Source "Perflib" -EventId 12345 -EntryType Error -Message $_
}
Start-Sleep -Milliseconds 100
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment