如何通过PowerShell仅导出AD组中指定列的成员数据
解决AD组成员导出指定列的问题
你的问题核心是未明确筛选要导出的属性列——即便在Get-ADUser中指定了-Property,PowerShell仍会返回该cmdlet的默认属性集合,必须通过Select-Object精准提取你需要的三列。
修改后的代码如下:
$groupNames = @("GroupName1", "GroupName2", "GroupName3", "GroupName4") For ($i=0; $i -lt $groupNames.Length; $i++) { $currentGroup = $groupNames[$i] $csvPath = "$currentGroup.csv" Get-ADGroup $currentGroup -Properties Member | Select-Object -Expand Member | Get-ADUser -Properties name, objectClass, distinguishedName | Select-Object name, objectClass, distinguishedName | # 新增此行筛选目标列 Export-CSV -Path $csvPath -NoTypeInformation }
额外提示
如果AD组内包含非用户对象(如其他组、计算机),Get-ADUser会触发报错,此时可改用Get-ADObject兼容所有对象类型,调整后代码如下:
$groupNames = @("GroupName1", "GroupName2", "GroupName3", "GroupName4") For ($i=0; $i -lt $groupNames.Length; $i++) { $currentGroup = $groupNames[$i] $csvPath = "$currentGroup.csv" Get-ADGroup $currentGroup -Properties Member | Select-Object -Expand Member | Get-ADObject -Properties name, objectClass, distinguishedName | Select-Object name, objectClass, distinguishedName | Export-CSV -Path $csvPath -NoTypeInformation }
内容的提问来源于stack exchange,提问作者noobCoder
相关产品推荐
相关产品推荐

