繁体   English   中英

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

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

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

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

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

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

$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM