csv 如何通过PowerShell仅选择要从AD组导出的特定列

pvabu6sv  于 2023-07-31  发布在  Shell
关注(0)|答案(1)|浏览(78)

我试图从一个AD组中获取组成员列表,并将其导出到csv文件。我有一个问题,我得到一个错误Get-ADGroupMember : The size limit for this request was exceeded,因为我的数据超过5k。我通过应用-Expand Member和使用Get-ADGroup GroupName -Properties Member解决了这个问题。数据被导出到csv,但是,我只想要3列。目前我正在获取组中的所有列。我尝试使用Get-ADUser -Property name, objectClass,distinguishedName,但它仍然输出组中的所有列。有没有办法只得到这三列?
这就是我的代码到目前为止的样子:

$groupNames = @("GroupName1", "GroupName2", "GroupName3", "GroupName4")

For ($i=0; $i -lt $groupNames.Length; $i++) {
    $currentGroup = $groupNames[$i]
    $csvPath = “$currentGroup.csv”

    Get-ADGroup $groupNames[$i] -Properties Member | Select-Object -Expand Member | Get-ADUser -Property name, objectClass,distinguishedName | Export-CSV -Path $csvPath -NoTypeInformation

    }

字符串

oyxsuwqo

oyxsuwqo1#

在输出到Csv以过滤感兴趣的属性之前,您缺少一个到Select-Object name, objectClass, distinguishedName的管道。这里有一个更有效的方法来执行您要做的事情,而不是查询每个member组,而是向Active Directory询问每个memberof组的所有用户:

$groupNames = @('GroupName1', 'GroupName2', 'GroupName3', 'GroupName4')

foreach ($group in $groupNames) {
    $csvPath = "$group.csv"
    $groupdn = (Get-ADGroup $group).DistinguishedName
    Get-ADUser -LDAPFilter "(memberof=$groupdn)" |
        Select-Object name, objectClass, distinguishedName |
        Export-Csv -Path $csvPath -NoTypeInformation
}

字符串

相关问题