I am wanting to see all nested groups inside a particular AD group. Doing this one level down is easy, but what I am needing is to see all nested groups. This means say we have an AD group and it has two groups inside of it that are nested and Im wanting to see all of the nested groups inside of these and then the nested groups inside of those nested groups. So in the end if I input it tells me every group that is nested and subnested inside of ,GroupA>.
Here is my code so far.
function get-nestedgroups{
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string]$group
)
Begin{
$ADGrp = Get-ADGroup -identity $group
$NestedGroups = Get-ADGroup -LDAPFilter "(&(objectCategory=group)(memberof=$($ADGrp.DistinguishedName)))"
}
Process{
if($NestedGroups.name -ne $null){
return $NestedGroups.SamAccountName
}
}
END{}
}
My thought is to create another function that takes in a group and runs this function on that group. then it takes the output groups from that function and does the same thing over and over until all of the nested groups are found but everytime I try and map it out I cannot figure out a solution to handle all of the foreach loops in a way that it will work with a while loop.