make sure you have these modules installed:
Install-Module -Name Microsoft.Graph.Intune
Install-Module -Name AzureAD
code:
Connect-MSGraph
Connect-AzureAD
# help functions
function get_parent_groups_from_group ($ObjectId){
$parents = Get-AADGroupMemberOf -groupId $ObjectId
return $parents
}
function get_child_groups_from_group($ObjectId){
Get-AzureADGroupMember -ObjectId $ObjectId | ? {$_.ObjectType -eq "Group"}
}
# get all the root groups
$root_groups = Get-AzureADGroup | ? {-not (get_parent_groups_from_group -ObjectId $_.ObjectID)}
# populate the children with recursion
function Recursion($groups, $max_depth, $current_depth=0){
if($current_depth -ge $max_depth){
return $groups
}
foreach($group in $groups){
write-host "$("`t" * $current_depth)$($group.displayname)"
$group | Add-Member -MemberType NoteProperty -Name children -Value "" -Force
$group.children = get_child_groups_from_group -ObjectId $group.ObjectId
if($group.children){
$group.children = Recursion -groups $group.children -current_depth ($current_depth + 1) -max_depth $max_depth
}
}
return $groups
}
$result = Recursion -groups $root_groups -max_depth 10
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…