Skip to content

Instantly share code, notes, and snippets.

@bill-long
Created August 29, 2018 23:47
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/6a8094665a846cc6a81e16dfb51101cd to your computer and use it in GitHub Desktop.
Save bill-long/6a8094665a846cc6a81e16dfb51101cd to your computer and use it in GitHub Desktop.
# Compare-GroupMembership.ps1
#
# Compares group membership across all GCs to verify replication.
param($Alias)
$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$allGCs = $forest.FindAllGlobalCatalogs()
$dictionary = New-Object 'System.Collections.Generic.Dictionary[string, System.Collections.Generic.HashSet[string]]'
foreach ($gc in $allGCs) {
$searcher = $gc.GetDirectorySearcher()
$searcher.Filter = "(mailnickname=" + $Alias + ")"
$result = $searcher.FindOne()
$members = New-Object 'System.Collections.Generic.HashSet[string]'
$offset = 0
do {
$countReturned = 0
$memberFinderResults = $null
$memberRangeProp = ("member;range=" + $offset + "-*")
$memberFinder = New-Object System.DirectoryServices.DirectorySearcher($result.Path, "(objectClass=*)", @("distinguishedName", $memberRangeProp), "Base")
try {
$memberFinderResults = $memberFinder.FindOne()
}
catch { }
foreach ($property in $memberFinderResults.Properties.Keys) {
if ($property.StartsWith("member;")) {
$countReturned = $memberFinderResults.Properties[$property].Count
foreach ($val in $memberFinderResults.Properties[$property]) {
$members.Add($val) | Out-Null
}
}
}
$offset += $countReturned
} while ($countReturned -gt 0)
Write-Host $gc ":" $members.Count "members"
$dictionary.Add($gc, $members)
}
# We have the group membership from all GCs
# Now get a list of all unique values
$uniqueValues = New-Object 'System.Collections.Generic.HashSet[string]'
foreach ($key in $dictionary.Keys) {
$uniqueValues.UnionWith($dictionary[$key])
}
Write-Host $uniqueValues.Count "members across all GCs"
foreach ($key in $dictionary.Keys) {
$membersOnThisGC = $dictionary[$key]
foreach ($val in $uniqueValues) {
if (!($membersOnThisGC.Contains($val))) {
Write-Host $key "missing value:" $val
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment