[英]Get email address from Samaccountname
我是一個動力貝殼的初學者。 我需要編寫一個命令,用於從活動目錄中獲取samccountname中的電子郵件地址。 我已將所有samaccountnames存儲在Users.txt文件中。
$users=Get-content .\desktop\users.txt
get-aduser -filter{samaccountname -eq $users} -properties mail | Select -expandproperty mail
請建議我如何繼續這樣做。 我在這里做錯了什么。
從文件中讀取后, $Users
成為$Users
的集合。 您無法將整個集合傳遞給過濾器,您需要一次處理一個用戶。 您可以使用ForEach循環執行此操作:
$users = Get-Content .\desktop\users.txt
ForEach ($User in $Users) {
Get-ADUser -Identity $user -properties mail | Select -expandproperty mail
}
這會將每個用戶的電子郵件地址輸出到屏幕。
根據評論,它也沒有必要使用-filter
,根據上述內容,您可以直接將samaccountname直接發送到-Identity
參數。
如果要將輸出發送到另一個命令(例如export-csv),則可以使用ForEach-Object:
$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,以便你可以交叉引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.