如何将所有组和组成员层次结构导入到csv中

时间:2017-01-13 15:26:36

标签: powershell active-directory

如何在树结构中使用PowerShell输出Active Directory组的成员资格?我使用的是Windows Server 2012.它应该将父组与子组匹配。

例如:

Groups     Member of
Group-1    Test group-1
           Test group-2
Group-2    Test group-3
           Test group-4

1 个答案:

答案 0 :(得分:0)

您可以使用Get-ADPrincipalGroupMembership获取群组对象的群组成员资格:

$Groups = Get-ADGroup -filter *
$output = foreach ($Group in $Groups) {
    $GroupMemberships = Get-ADPrincipalGroupMembership $Group
    if ($null -ne $GroupMemberships) {
        foreach ($GroupMembership in $GroupMemberships) {
            [pscustomobject]@{
                Group = $Group.Name
                Memberof = $GroupMembership.Name
            }
        }
    } else {
            [pscustomobject]@{
                Group = $Group.Name
                Memberof = "None"
            }
    }
}
$output | Format-Table
$output | export-csv C:\example.csv -notypeinformation