2016-10-01 65 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!