powershell - Get all Groups of a user/group recursively
问题描述
I have some code to get the groups of a user and write them down into an Arraylist, however ît will only find the groups where a user is directly in. It won't find groups deeper then 1 level.
For example: User is member of Group 1, Group 1 is member of Groups 2, etc. I will only find Group 1. Group 2 won't be written down into my ArrayList.
$Groups = Get-ADPrincipalGroupMembership -Server ESX-DC $GroupName
$GroupArrayList = New-Object System.Collections.ArrayList
foreach ($Group in $Groups)
{
$GroupArrayList.Add($Group.Name) | Out-Null
}
Can someone provide me some help here? Thanks.
解决方案
我不知道递归参数,所以我认为您必须自己编写。然而,我为类似的任务编写了一个脚本。可能会有所帮助。
function Get-ADPrincipalGroupMembershipRecurse
{
param
(
[Parameter(Mandatory = $true)]
[System.String]$Identity
)
$script:Groups = @()
function Get-NestedAdGroups
{
param
(
[Parameter(Mandatory = $true)]
[System.String]$Identity
)
$ADGroup = Get-ADGroup -Identity $Identity -Properties MemberOf, Description
$script:Groups += $ADGroup
foreach ($Group in $ADGroup.MemberOf)
{
if ($script:Groups.DistinguishedName -notcontains $Group)
{
Get-NestedAdGroups -Identity $Group
}
}
}
foreach ($Group in (Get-ADUser -Identity $Identity -Properties MemberOf).MemberOf)
{
Get-NestedAdGroups -Identity $Group
}
return ($script:Groups | Sort-Object -Unique)
}
Get-ADPrincipalGroupMembershipRecurse -Identity $SamAccountName
推荐阅读
- node.js - 连接到 Mongodb 的可变路由/url 参数
- android - 如何在 Android 中播放 Base 64 编码的音频
- ansible - 如何将值列表动态传递给 Ansible URI 模块
- flutter - 等效于 Javascript 的 Math.sign 但适用于 Dart
- java - 在java文件中搜索行并使用批处理脚本输出到另一个文件
- c - 在我的内核驱动程序中是“reinterpret_cast”未定义
- search - 如何在 Solr 上处理阿拉伯字符
- reactjs - 用于 Firebase 的 Ckeditor 自定义文件上传适配器不起作用
- sql - 多对多关系的两个以上表
- c++ - cmake target_compile_options 工作,但 add_compile_options 没有