2013-10-07 18 views
0

我試圖找到用戶,在AD中的 'users.csv' 存儲:PowerShell的:進口的CSV和Get-ADUser便有

Import-Module ActiveDirectory 
import-csv "\users.csv" | ForEach-Object { 
Get-ADUser -Filter {displayname -eq $_.id}} 

但PS說,沒有​​發現 '身份證' 屬性

用戶的.csv內容:

id 
MacAskill Danny 
Cedric Gracia 

回答

3

編輯CSV文件,並附有像這樣的雙引號的displaynames,

id 
"MacAskill Danny" 
"Cedric Gracia" 

之後,使用itermediate變量像這樣,

Import-Csv .\users.csv | % { 
    $f = $_.id; # Set the displayname into a temp variable 
    get-aduser -filter { displayname -eq $f } # Use the temp variable with filter 
} 

我卻不知道爲什麼需要一個intermeidate變量。用-filter參數傳遞和解析變量有一些奇怪的事情發生。

+0

'%'的概念是什麼?第一天使用PS,它吹了我的腦...) – nuT707

+0

@ nuT707,'%'是'Foreach-Object'的別名。也就是說,它只是一個較短的形式。就像'?'是'Where-Object'的快捷方式。更多信息:'get-help about_aliases'。 – vonPryz

1
# Use Import-csv and Get-ADUser together 
# Import csv that contains "sn" column and get all AD users where 
# sn matches any of the values imported from csv 
Import-Csv C:\temp\1.csv | select sn -ExpandProperty sn | foreach { Get-ADUser -Filter 'sn -eq $_' } 
+1

您可能想要編輯您的答案以便閱讀... – leo

0

我知道這是一箇舊帖子,但它確實爲我節省了很多頭痛。無法弄清楚如何使用導入的值查找用戶,但只需創建一箇中間變量,我就可以使我的腳本正常工作。我甚至不需要使用任何雙引號。

克里斯