2016-10-01 30 views
0

我想我遇到了foreach循環處理的問題。但我不明白爲什麼不工作。所以我得到了未被發現的結果。我嘗試了幾個變化,但沒有運氣。導出問題與foreach和Get-ADUser -Filter

$reportdate = Get-Date -Format ssddmmyyyy 
$csvreportfile = "c:\export\ALLADUsers_$reportdate.csv" 

Import-Csv -Path "c:\export\list.csv" | ForEach-Object { 
    Get-ADUser -Properties * -Filter "EmployeeId -eq $($user.EmployeeID)" | 
     Select-Object @{Label = "First Name";Expression = {$_.GivenName}}, 
      @{Label = "Last Name";Expression = {$_.Surname}}, 
      @{Label = "Display Name";Expression = {$_.DisplayName}}, 
      @{Label = "Logon Name";Expression = {$_.sAMAccountName}}, 
      @{Label = "WhenCreated";Expression = {$_.whenCreated}}, 
      @{Label = "Full address";Expression = {$_.StreetAddress}}, 
      @{Label = "City";Expression = {$_.City}}, 
      @{Label = "State";Expression = {$_.st}}, 
      @{Label = "Post Code";Expression = {$_.PostalCode}}, 
      @{Label = "Country/Region";Expression = {if (($_.Country -eq 'GB')) {'United Kingdom'} else {''}}}, 
      @{Label = "Job Title";Expression = {$_.Title}}, 
      @{Label = "Company";Expression = {$_.Company}}, 
      @{Label = "Description";Expression = {$_.Description}}, 
      @{Label = "Department";Expression = {$_.Department}}, 
      @{Label = "Office";Expression = {$_.OfficeName}}, 
      @{Label = "Phone";Expression = {$_.telephoneNumber}}, 
      @{Label = "Email";Expression = {$_.Mail}}, 
      @{Label = "Manager";Expression = {%{(Get-ADUser $_.Manager -Server $ADServer -Properties DisplayName).DisplayName}}}, 
      @{Label = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')) {'Enabled'} else {'Disabled'}}, # the 'if statement# replaces $_.Enabled 
      @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}} 
} | Export-Csv -Path $csvreportfile -NoTypeInformation 

假設輸入CSV是這樣的:

EmployeeID 
16507 
16534 
16587 
16622 
16631 
16632 
16659 
16661 
16663 
16666 
16688 
16713 
16728 
16753 
16830

輸出CSV:

"First Name","Last Name","Display Name","Logon Name","WhenCreated","Full address","City","State","Post Code","Couny/Region","Job Title","Company","Description","Department","Office","Phone","Email","Manager","Account Status","Last LogOn Date" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso.","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
"Jax","Teller","Jax Teller","JaxT","09.11.2012 12:34:35",,"Foobar","13685",,"","Consultant","Contoso","Contoso/ 
- ","Research","","1111111111 (1111)","[email protected]",,"Enabled","16.08.2016 15:50:27" 
+0

嗯......那麼,什麼*是*「意外的結果」?代碼「不工作」怎麼樣?假設它是過濾器表達式:請看看[這個問題](http://stackoverflow.com/q/34028164/1630171)。 –

+0

我的意思是「意外結果」,它返回一個CSV文件中未包含的重複employeeid用戶,例如13685 ID – Arbelac

+0

當您說「重複」時,您的意思是什麼?您是如何驗證員工編號不正確的,因爲您沒有在輸出中包含該信息?一般來說,請提供更多信息讓我們瞭解問題。 –

回答

0

現在我明白了。您在過濾器中使用了一個變量$user,該變量似乎在之前的某個位置已被填充。這意味着對於每個輸入ID,您實際上會獲取相同的用戶對象,而不是具有給定ID的對象。 13685只是看起來是該用戶帳戶的st屬性的值。

$user替換爲當前對象變量$_並且問題將消失。

Import-Csv 'C:\export\list.csv' | ForEach-Object { 
    Get-ADUser -Properties * -Filter "EmployeeId -eq $($_.EmployeeID)" | 
     Select-Object ... 
} | Export-Csv $csvreportfile -NoType 

作爲一個側面說明,使用計算特性。只有當你真正做一些與屬性(如重命名的財產,變更/重新格式化的值等)。否則,只需將屬性名稱放在列表中。把一個物業的價值放到一個計算出來的財產中是完全沒有意義的:

@{Label = "City";Expression = {$_.City}} # <- useless!