The base algorithm of what you're looking for looks something like this (according to me, anyway):
- Enumerate all of the group with the right prefix.
- Recursively follow their GroupMembers to enumerate any nested groups in them.
- Once you get a group with no groups as members, stop recursing.
- Take the list of users, for each user
- Enumerate their Group Memberships
- If any have the right prefix, remove them from it.
- If any of the remaining groups are in the list of groups enumerated in the previous major step, remove them from that one too.
Now for actual code-like things.
Enumerating all groups with a prefix (untested, there will be bugs):
$RecurseList=dsquery group -name "abc-*"
$TargetList=$RecurseList
foreach $Grp in $RecurseList {
# Now get the members of that group, do not expand
$GrpMembers=dsget group "$Grp" -members
foreach ($Member in $GrpMembers) {
$isGroup=dsget group $Member
if ($isGroup.dn -eq $Member) {
$TargetList.add("$Member")
RecurseIntoGroup($isGroup.dn)
}
}
}
Then when it comes time to talk the CSV list, get the membership of the user, and check to see if that group exists in $TargetList above. If so, remove it.
This is a heck of a lot of work to go through when removing just one user from potentially thousands of groups, but if you're doing a LOT of these then having the pre-built list will save you time.
If you only need to do it for a few users (say, 10 or so), you can walk back up the tree.
$UserGroups = dsquery user -name $Username -memberof
foreach ($uGroup in $UserGroups) {
if (isConcerning($uGroup)) {
$ConcerningGroups.add("$uGroup")
}
}
function isConcerning {
param ($uGroup)
$parentGroups=dsget group $uGroup -memberOf
$found=$False
foreach ($pg in $parentGroup) {
if ($parentGroup.startswith("abc-")) {
return($true)
$found=$true
} else {
$concerning=isConcerning($pg)
if ($concerning) {
return($true)
$found=$true
}
}
}
if (-not $found) {
return($False)
}
And then remove the concerning groups as needed.
you can try mine which works for me. You need to set all the variables, which should all self-documenting.
$body = "$(cat $file)"
send-MailMessage -SmtpServer $smtpserver -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high
Best Answer
How about...
You will need to adjust to taste, and work out if you need directory removal, or content deletion, or more, but that should do for the date part, anyway.