powershell - 在 powershell 中获取所有 AzureAD 组的 GroupTree
问题描述
我想在 powershell 中获取我所有 azureAD 组的树视图。我没有找到任何公开的代码。在我自己开始编写之前,是否有人为此提供了一些代码?
所以输出类似这样的东西:
Group1
Group2
Group3
ChildOfGroup3
2ndChildOfGroup3
ChildOfChild
Group4
ChildofGroup4
解决方案
确保您已安装这些模块:
Install-Module -Name Microsoft.Graph.Intune
Install-Module -Name AzureAD
代码:
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
推荐阅读
- python-3.x - 使用 Visual Studio 代码 Python 3.8.5 打印时出现语法错误
- python - 从用户获取日期输入并使用python作为参数传递
- xamarin - System.NotSupportedException:'无法从本机句柄激活 SkiaSharp.Views.Forms.SKCanvasViewRenderer 类型的实例'
- scala - 一个动作返回 Future[Result] 而其他返回 Accumultor[]
- typescript - 当我们在vs代码工作区中保存文件时如何获取新修改行的行号?
- c# - 在 .Net Standard 2.0 中替换 string.Contains(string, StringComparison)
- augmented-reality - Reality Converter Beta 3 – GLTF 到 USDZ 转换中的错误
- spring-boot - Spring Boot app从jar包启动失败,但在IDE中运行正常
- windows - Wireshark Windows 构建失败:找不到 POD(缺少:POD2MAN_EXECUTABLE POD2HTML_EXECUTABLE)
- python - 是否可以使用 google vision api 一次扫描 10 张图像 ocr?到目前为止只做了1