PowerShell列出来自特定组织单位的多个AD组成员身份

时间:2017-06-08 18:49:25

标签: powershell active-directory membership active-directory-group ou

我需要从特定的AD OU中获取所有AD组及其各自的成员资格。我写的代码在写入主机屏幕但我无法将其干净地输出到.xml(这是请求的格式),但此时我会以任何格式使用它。管理层希望输出文件类似于:

Group1 User1
Group1 User2
Group1 User3 
Group2 User1
Group2 User2
Group2 User3
etc...

有人有什么想法吗?

$groupName = Get-ADGroup -SearchBase "OU=Groups,DC=domain,DC=com" -Filter * | Select-Object sAMAccountName

foreach ( $groupMember in $groupName )
{
   # $groupMember
   Get-ADGroupMember -Identity $groupMember.sAMAccountName | Export-Clixml -Path C:\temp.xml -Force

2 个答案:

答案 0 :(得分:0)

假设您希望显示与您一样的两列文本文件,这不是XML格式的文件。您可以使用(' Select-Object,然后使用选择的格式导出该信息:

$GroupName = Get-ADGroup -SearchBase "OU=Groups,DC=domain,DC=com" -Filter * | Select-Object -ExpandProperty sAMAccountName

$Members = foreach ($GroupMember in $GroupName) {
   Get-ADGroupMember -Identity $GroupMember | Select-Object @{Name="Group";Expression={$GroupMember}},name
}
"$(Members.Group) $($Members.Name)"  | Out-File C:\temp.txt
#Alternatively for a .CSV
$Members | Export-CSV C:\temp.csv

答案 1 :(得分:0)

我相信你在"(Members.Group)"前面错过了$第5行chr 3不应该是" $($ Members.Group)"代替?因为当我运行这个脚本时,它给了我错误'“会员组”这一术语'不被识别为cmdlet的名称,函数....'

$GroupName = Get-ADGroup -SearchBase "OU=Groups,DC=domain,DC=com" -Filter * | Select-Object -ExpandProperty sAMAccountName

$Members = foreach ($GroupMember in $GroupName) {
   Get-ADGroupMember -Identity $GroupMember | Select-Object @{Name="Group";Expression={$GroupMember}},name
}
"$($Members.Group) $($Members.Name)"  | Out-File C:\temp.txt
#Alternatively for a .CSV
$Members | Export-CSV C:\temp.csv