从Samaccountname获取电子邮件地址
问题描述:
我是初学者的权力外壳。我需要编写一个用于从活动目录中获取samccountname的电子邮件地址的命令。我已将所有samaccountnames存储在Users.txt文件中。从Samaccountname获取电子邮件地址
$users=Get-content .\desktop\users.txt
get-aduser -filter{samaccountname -eq $users} -properties mail | Select -expandproperty mail
请告诉我该如何继续下去。我在这里做错了什么。
答
从文件中读取后,$Users
成为用户的集合。您无法将整个集合传递给过滤器,您需要一次处理一个用户。你可以用一个foreach循环做到这一点:
$users = Get-Content .\desktop\users.txt
ForEach ($User in $Users) {
Get-ADUser -Identity $user -properties mail | Select -expandproperty mail
}
这将输出每个用户的电子邮件地址到屏幕上。
根据评论,它也没有必要使用-filter
为此,根据上述您可以直接发送samaccountname到-Identity
参数。
如果你想在输出发送到另一个命令(如出口CSV),你可以使用的foreach对象,而不是:
$users = Get-Content .\desktop\users.txt
$users | ForEach-Object {
Get-ADUser -Identity $_ -properties mail | Select samaccountname,mail
} | Export-CSV user-emails.txt
在这个例子中,我们使用$_
来表示当前项目管道(例如用户),然后我们将命令的输出传送到Export-CSV。我以为你可能也希望这种输出具有samaccountname和mail,以便你可以交叉引用。
您不需要使用'-filter'参数通过'sAMAccountName'检索'ADUser'; '-Identity'参数将采用'sAMAccountName'作为有效值:'Get-ADUser -Identity $ sAMAccountName -Properties mail |选择对象 - 属性邮件' –
好点!我会修改我的答案。 –