[英]Using Powershell, how to get a list of Active Directory group members, including users and contacts, showing name, company, and email
I'm trying to export lists of AD group members with powershell.我正在尝试使用 powershell 导出 AD 组成员的列表。 I was doing great with commands like this:我对这样的命令做得很好:
Get-ADGroupMember "MyGroupName" -Recursive | Get-ADUser -Properties Company,Surname,GivenName,EmailAddress | select Company,Surname,GivenName,EmailAddress | Sort-Object -Property Company,Surname | Export-CSV $home\Desktop\MyGroupName.csv
Then I realized that I was only getting users and not getting contacts, and I need both.然后我意识到我只获得用户而不获得联系人,我需要两者。 I spent a pile of time Googling for how to include contacts as well as users, delving into Get-ADObject and filtering with ObjectClass "contact", but I can't seem to find a simple way to dump a list of group members that includes both users and contact and displays the info I want.我花了很多时间在谷歌上搜索如何包括联系人和用户,深入研究 Get-ADObject 并使用 ObjectClass“联系人”进行过滤,但我似乎找不到一种简单的方法来转储包含的组成员列表用户和联系人都显示我想要的信息。
One suggestion online was to use网上的一个建议是使用
(Get-ADGroup "MyGroupName" -Properties members).members
That gives me the DistinguishedNames of the members, including both users and contacts, but I can't figure out how to get the properties I want.这给了我成员的 DistinguishedNames,包括用户和联系人,但我不知道如何获得我想要的属性。 The property names between contacts and users don't really align - mail vs. EmailAddress, etc. Also, piping that output to Get-ADUser, unsurprisingly throws errors on the contacts.联系人和用户之间的属性名称并没有真正对齐 - 邮件与电子邮件地址等。此外,将 output 管道传输到 Get-ADUser,不出所料会在联系人上引发错误。
If I pipe it to something like this:如果我 pipe 它是这样的:
Get-ADObject -Filter 'objectClass -eq "contact"' -Properties CN,mail,company | Format-Table CN,mail,company
I get the info I want on the contacts, but it throws errors on all of the users.我得到了我想要的联系人信息,但它会在所有用户身上引发错误。 Any advice/assistance would be appreciated.任何建议/帮助将不胜感激。 Thanks!谢谢!
You'll need to process Users separately from Contacts, more or less.您或多或少需要将用户与联系人分开处理。 This isn't tested or complete, but should point the way for you:这没有经过测试或完成,但应该为您指明方向:
(Get-ADGroup "MyGroup" -Properties Members).Members | Get-ADObject | ForEach-Object {
if ($_.ObjectClass -eq "contact") {
#emit what you want for a contact
} elseif ($_.ObjectClass -eq "user") {
#emit what you want for a user
} else { #it's probably a group, but ...
#process whatever isn't a user or a contact
}
}
What this does when you fill in the comments with your real code is当你用你的真实代码填写评论时,它的作用是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.