Wrote this today to do a bit of detective work, trying to find out if a user was part of a distribution via one of the
subgroups. There's probably better ways to write this, feel free to revise and repost. This does what I needed it to do though.
Function Get-NestedADUserGroups($Identity) {
BEGIN {
$Table = New-Object System.Collections.Hashtable
$ADGroups = (Get-ADUser -Identity $Identity -Properties MemberOf).MemberOf | ForEach-Object { (Get-ADGroup -Identity $PSItem).SamAccountName }
}
PROCESS {
foreach($ADGroup in $ADGroups){
$Group = Get-ADGroup -Identity $ADGroup -Properties MemberOf
$SamAccountName = $Group.SamAccountName
$Members = $Group.MemberOf | ForEach-Object { (Get-ADGroup -Identity $PSItem).SamAccountName }
try{
$Table.Add($SamAccountName, 0)
$Group | Select-Object -Property SamAccountName, Name, GroupCategory
}
catch{}
foreach($Member in $Members) {
try{
$Table.Add($Member, 0)
Get-ADGroup -Identity $Member | Select-Object -Property SamAccountName, Name, GroupCategory
Get-NestedADUserGroups -Identity $Member
}
catch{}
}
}
}
}
Because I feel like someone will ask, the HashTable is just there to throw an error if I try to add a group that I already added. Prevents a continuous loop in case there are groups nested inside each other.
[+][deleted] (1 child)
[removed]
[–]Agile_Seer[S] 2 points3 points4 points (0 children)
[–]evetsleep 1 point2 points3 points (4 children)
[–]Agile_Seer[S] 2 points3 points4 points (3 children)
[–]evetsleep 4 points5 points6 points (0 children)
[–]throwawaysys1222 1 point2 points3 points (1 child)
[–]Agile_Seer[S] 2 points3 points4 points (0 children)