如何使用 powershell 从 AD 中的组获取​​嵌套组名

How to get nested group name from groups in AD using powershell

我有一个问题需要在 Active Directory 中使用 powershell 以递归方式获取属于某个组的所有组,依此类推。

因此,如果您的 A 组有 B,B 有 C,那么我需要这些组的名称,包括 A。

另外,如果 C 也有 A,如何解决以下问题我不希望它进入无限循环。

到目前为止,这是我的代码。

$groupname = "a","b","c"

foreach($grouphp in groupname)
{
$groupname += (get-adgroupmember -server $domain -identity $grouphp | where-object {$_.objectclass -like "group"}).name

}

这会将它们添加到 $groupname 1 级别,如果找不到组,并且如果找到的组也是我正在寻找的组,它也会多次列出它们。

感谢任何帮助。

您可以创建一个列表来进行跟踪,然后仅在之前未添加的情况下添加。

$groupname = "a","b","c"

$grouplist = New-Object System.Collections.Generic.List[string]

foreach($group in $groupname)
{
    if($group -notin $grouplist){$grouplist.Add($group)}

    Get-ADGroupMember -server $domain -Identity $group |
        Where-Object objectclass -like "group" | ForEach-Object {
            if($_.name -notin $grouplist){$grouplist.Add($_.name)}
    }
}

$grouplist

请使用以下脚本从嵌套组中获取用户:

https://gallery.technet.microsoft.com/Get-nested-group-15f725f2