Powershell get-adgroupmembership文本文件

时间:2013-12-12 14:14:24

标签: powershell powershell-v2.0

目前我有:

import-module activedirectory
$path = get-content "E:\test.txt" | Out-String
$path | ForEach-Object { Get-ADGroupMember $_ | Select-Object name }

这将获取文本文件中指定的组内用户的名称(lastname_firstInitial)。 文本文件看起来像:

groupname1
groupname2
...
groupname50

有没有办法输出用户的全名" displayname" property,get-adgroupmember不支持-property displayname或firstname&姓氏 - 我还需要将名称显示在他们被拉出的正确组旁边。

目前我只能检索"登录名称"但不知道他们是从哪个组中抽出来的。

谢谢。

菲尔

1 个答案:

答案 0 :(得分:2)

发出此命令时,您将获得所有成员的列表,每个成员都包含AD中的唯一ID。您可以使用它来获取实际的ADUserObject并从那里开始工作。

$Filename = "C:\temp\groups.txt"

#ForEach ( $GroupName in [System.IO.File]::ReadLines($Filename) ) {
 ForEach ( $GroupName in (Get-Content $Filename) ) {
     $UserIDs = (Get-ADGroupMember $GroupName).ObjectGUID

     ForEach ( $UserID in $UserIDs ) {
          $ADUser = Get-ADUser $UserID -Properties DisplayName
          Write-Output "$GroupName : $($ADUser.DisplayName)"
    }
}