获取AD组及其用户

时间:2016-12-19 18:29:36

标签: powershell scripting active-directory

我一直在尝试获取AD环境中所有组的列表(包含说明)及其成员,并将其输出到CSV文件。理想情况下,用户将显示在他们的组下。我一直试图使用的脚本是:

Import-Module ActiveDirectory

Get-ADGroup -Filter * -Properties Description |
    Select-Object Name, Description |
    ForEach-Object {
        Get-ADGroupMember -Identity $_.DistinguishedName -Recursive |
            Get-ADObject -Properties SamAccountname, Title, Department |
            Select-Object Name, SamAccountName, Title, Department, DistinguishedName, ObjectClass
    } | Export-Csv -Path c:\temp\ADGrab.csv -NoTypeInformation

我一直得到的错误如下:

Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument
is null or empty. Supply an argument that is not null or empty and then try the
command again.
At C:\Users\j_kennedy_ta\AppData\Local\Temp\9\2898ceb2-a6cf-4fbf-9341-e651dad2145d.ps1:4 char:28
+ Get-ADGroupMember -Identity <<<<  $_.distinguishedname -Recursive |
    + CategoryInfo          : InvalidData: (:) [Get-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember

1 个答案:

答案 0 :(得分:1)

没有令人讨厌的Import-Module ActiveDirectory Get-ADGroup -Filter * -Properties Description | ForEach-Object { # Store for later use $groupName = $_.Name $groupDescription = $_.Description Get-ADGroupMember -Identity $_.DistinguishedName -Recursive | Get-ADObject -Properties SamAccountname, Title, Department | Select-Object Name, SamAccountName, Title, Department, DistinguishedName, ObjectClass, ` # Mind the gap # Calculated properties with group information @{ name = "GroupName"; expression = $groupName }, ` @{ name = "GroupDescription"; expression = $groupDescription } } | Export-Csv -Path c:\temp\ADGrab.csv -NoTypeInformation 以及CSV文件中的群组信息:

<?php readfile("file.html"); ?>