【问题标题】:Powershell get-adgroupmembership text filePowershell get-adgroupmembership 文本文件
【发布时间】:2013-12-13 00:02:00
【问题描述】:

目前我有:

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”属性的全名,get-adgroupmember 不支持 -property displayname 或 firstname & last name - 我还需要将名称显示在正确的组旁边拉出来的。

目前我只能检索“登录名”,但不知道他们是从哪个组中提取的。

谢谢。

菲尔

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    发出此命令时,您将获得所有成员的列表,每个成员都包含 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)"
        }
    }
    

    【讨论】:

    • 为什么是[System.IO.File]::ReadLines 而不是get-content?除非您处理的是非常大的文件,否则差异可以忽略不计
    猜你喜欢
    • 1970-01-01
    • 2014-04-17
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多