如何使用 PowerShell 检索 AD 组中每个人的电子邮件别名和 CN
我将 PowerShell 与 Quest AD cmdlet 结合使用。
我可以使用 Get-QADGroupMember cmdlet 来获取给定组中每个人的列表。到目前为止一切顺利,但我也想得到他们的电子邮件别名。目前返回的所有内容都是这样的:
Name Type DN
---- ---- --
Jane Doe User CN=Jane Doe,OU=Employee,DC=companyname,DC=com
Job Blow User CN=Joe Blow,OU=Employee,DC=companyname,DC=com
我尝试使用 get-qaduser 和 -includeallproperties 标志,但我仍然只返回上述字段,并且我不知道如何获取返回的数据,文档说这些数据缓存在电脑。
任何帮助将不胜感激。
更新
我最终使用了类似于下面的“select”:
$everyone = Get-QADGroupMember "All employees" | select firstname, lastname, email
这将我需要的所有内容都放入了哈希表数组中。那时,通过使用如下代码迭代每个人,可以很容易地完成所需的任何操作:
for ($i=0; $i -le $everyone .length-1; $i++)
{
write-host $everyone[$i].email
}
花了我很长时间才找到“。”用于从哈希表中提取特定值的符号。我进行了文本解析,效果很好,但我知道这不是正确的方法,并最终找到了有关点表示法的文档。我希望在这里记录下来可以节省其他人的时间!
I am using PowerShell with the Quest AD cmdlets.
I can use the Get-QADGroupMember cmdlet to get a list of everyone in a given group. So far so good but I would like to get their email alias as well. All that is returned currently is something like:
Name Type DN
---- ---- --
Jane Doe User CN=Jane Doe,OU=Employee,DC=companyname,DC=com
Job Blow User CN=Joe Blow,OU=Employee,DC=companyname,DC=com
I tried using get-qaduser with the -includeallproperties flag but I still only get the above fields returned and I don't know how to get at the returned data which the documentation says is cached on the computer.
Any help would be appreciated.
UPDATE
I ended up using "select" similar to below:
$everyone = Get-QADGroupMember "All employees" | select firstname, lastname, email
And that got everything I needed into an array of hashtables. At that point it is easy to do whatever is needed by iterating through everyone with code like:
for ($i=0; $i -le $everyone .length-1; $i++)
{
write-host $everyone[$i].email
}
Took me forever to find the "." notation for pulling specific values out of the hashtable. I did text parsing and that worked but I knew that couldn't be the right way of doing it and eventually found documentation on the dot notation. I hope documenting that here saves someone else some time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定它真的不返回该信息吗?您是否尝试过将命令通过管道传输到
Get-Member
或Format-List -Force *
中? PowerShell 可以配置为仅显示项目的一些属性,而不是此处可能出现的所有情况。您可以使用
Select-Object
选择属性,或者如果您知道它们在那里,则只需select
选择属性,即使 PowerShell 默认情况下不显示它们:例如,您可以使用
Get-ChildItem
也是如此:正常调用仅产生四个属性:
Mode
、LastWriteTime
、Length
和名称
。然而,正如Get-Member
所示,还有更多。Are you sure it really doesn't return that information? Have you tried piping the command into
Get-Member
orFormat-List -Force *
? PowerShell can be configured to only show a few properties of items and not all which might be the case here.You can select properties using
Select-Object
or justselect
if you konw they are there, even though PowerShell doesn't display them by default:You can see this for example with
Get-ChildItem
too:The normal invocation only yields four properties:
Mode
,LastWriteTime
,Length
andName
. However, there are plenty more, asGet-Member
shows.请记住,选择对象会剥离对象并生成新对象。
因此,在此示例中:
$test 将是仅包含名称的 PSCustomObject (System.Object)。
您想用这些数据做什么?输出到控制台...到文件?
我会做这样的事情:
或者
Rememember that select-object strips down the object and generates new ones.
So in this example:
$test will be a PSCustomObject (System.Object) containing only the name.
What do you want do do with the data? Output to the console...to a file?
I would do something like this:
Or