The script writes the information to a file for better reviewing later. The output adds indentation to help readability.
Here is my solution.
#
# Enumerate All Groups for a User (including traversing nested groups)
# @param distinguishedName - The distinguished name of the object you want to traverse
# Brock Moeller
# 12/16/2009
#
param (
$distinguishedName = "CN=Joe Dirt,OU=Users,DC=serv,DC=ubercorp,DC=com"
)
$roles = @{};
$indent = -1;
filter EnumRoles {
$indent += 1;
if ($_ -is [System.DirectoryServices.DirectoryEntry]){
$adsiObj = $_;
} else {
$adsiObj = New-Object System.DirectoryServices.DirectoryEntry("LDAP://" + $_);
}
if ((-not [String]::IsNullOrEmpty($adsiObj.cn)) -and (-not $roles.ContainsKey($adsiObj.cn))){
$roles[$adsiObj.cn] = 1;
$memberOfCount = $adsiObj.memberOf.Count;
$("`t"*$indent) + $adsiObj.cn + " [$memberOfCount]";
if (($memberOfCount -gt 0) -and ($indent -lt 900)) {
$adsiObj.memberOf | EnumRoles;
}
}
$indent -= 1;
}
$user = [adsi]"LDAP://$distinguishedName";
$user;
$buffer = $user.Path + "`n";
$user | EnumRoles | % { $buffer += $_ + "`n" };
"Buffer: " + $buffer.ToString();
[System.IO.File]::WriteAllText("$pwd\$($user.cn).txt", $buffer.ToString());
No comments:
Post a Comment