2016-10-01 10 views
0

foreachループ処理で問題が発生していると思います。しかし、私はそれがなぜ機能しないのか理解していません。だから私は未成熟の結果を得ている。私はいくつかのバリエーションを試しましたが、運はありません。Export with foreachとGet-ADUser -Filter

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

13685などのCSVファイルに含まれていない重複したemployeeidユーザーを返す「予期しない結果」を意味しますID – Arbelac

+0

「複製」と正確には何を意味しますか?その情報を出力に含めないので、従業員IDが間違っていることをどのように確認しましたか?一般的には、問題を理解するための情報を提供してください。 –

答えて

0

今私が見る

$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が似ている入力を想定します。フィルタ内に変数$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!