簡體   English   中英

Powershell查詢AD以導出.txt文件中所有用戶的電子郵件?

[英]Powershell Query AD to export emails of all users in .txt file?

我想編寫一個PS腳本,為單獨的.txt文件中指定的所有用戶導出.csv。 因此,例如,我可以創建一個具有的文本文件

Timmy Turner
Silly Sally

然后,當腳本運行時,它會搜索AD以獲取這兩個用戶,並導出帶有其名字,姓氏和電子郵件地址的CSV。

我最初對“Get-ADUser”過濾器的工作方式有所了解,但我制作了一些半可用的東西。 但是,我想出來的只是問你要搜索的是誰然后使用它。 我認為讓它引用預先制作的文本文件要容易得多,特別是在搜索大量用戶時。 或者,可能有一種更簡單的方法來做到這一點,我沒有想到。 這是我目前擁有的:

$SamAc = Read-Host 'What is the first and last name of the person you would like to search for?'
$filter = "sAmAccountname -eq ""$SamAc"""

Get-ADUser -Filter $filter -Properties FirstName, LastName, EmailAddress | select FirstName, LastName, EmailAddress | Export-CSV "C:\Scripts\PS_ADQuery\Email_Addresses.csv" 

我覺得“Get-Content”cmdlet接近我正在尋找的東西,但我似乎無法讓它正常運行。 不過,我可能會走向完全錯誤的方向。

我找到了答案。 事實證明,我甚至將AD屬性完全錯誤。 因為我可能不完全理解每行代碼背后的過程,所以我的評論很多,但這正是我想要做的。

#creates a $users variable for each username listed in the users.txt file. the ForEach
#command allows you to loop through each item in the users.txt array. the scriptblock 
#nested into the ForEach command queries each username for specific properties.
$users = ForEach ($user in $(Get-Content C:\Scripts\PS_ADQuery\users.txt)) {

    Get-AdUser $user -Properties GivenName,sn,mail

}

#takes the $users variable defined by the ForEach command and exports listed properties 
#to a csv format.     
 $users |
 Select-Object GivenName,sn,mail |
 Export-CSV -Path C:\Scripts\PS_ADQuery\output.csv -NoTypeInformation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM