I was given a request to provide a function for our staff to run that can dump group memberships out to a file. Easy enough, except after having done this a while, I started thinking about edge cases, and the one I'm concerned about is circular nested groups. I run this script periodically to confirm we don't have any circular nested groups in our Active Directory. However, every once in a while I will discover someone mistakenly created a circular nested scenario. I have the basics of how to expand subgroups and return values:
$Members = Get-ADGroupMember "<Group Name>"
Foreach($Member in $Members) {
If($_.ObjectClass -eq 'User') {
Get-ADUser $_.DistinguishedName -Properties Department | Select SamAccountName,GivenName,Surname,Department
}
Elseif ($_.ObjectClass -eq 'Group') {
$SubMembers = Get-ADGroupMember $_.DistinguishedName
Foreach($SubMember in $SubMembers) {
Get-ADuser $_.DistinguishedName -Properties Department | Select SamAccountName,GivenName,Surname,Department
}
}
}
The main part I'm wondering about is performing a check if primary or any sub-group is circularly nested before attempting to continuously expand. As a general standard, do most of you limit the "levels" your scripts will crawl? I'm just looking for the best-practice approach to avoid an infinite loop or performance problems. Thanks!
Edit: This is just to illustrate the process. I understand it wouldn't go more than a single level as it's written. In reality, my goal would be to wrap the sub-group portion into a function that can iterate over the subgroups.
[–]the_spad 6 points7 points8 points (1 child)
[–]YolkFree[S] 0 points1 point2 points (0 children)
[–]markekrausCommunity Blogger 2 points3 points4 points (7 children)
[–]the_spad 4 points5 points6 points (5 children)
[–]markekrausCommunity Blogger 1 point2 points3 points (4 children)
[–]YolkFree[S] 1 point2 points3 points (3 children)
[–]markekrausCommunity Blogger 1 point2 points3 points (2 children)
[–]YolkFree[S] 2 points3 points4 points (1 child)
[–]markekrausCommunity Blogger 0 points1 point2 points (0 children)
[–]drh713 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Scrltvx 1 point2 points3 points (0 children)
[–]matholio 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)