使用foreach和Get-ADUser -Filter导出问题

时间:2016-10-01 12:23:24

标签: powershell csv export

我认为我对foreach循环处理存在问题。但我不明白为什么不工作。所以我得到了未经检测的结果。我尝试了几种变化,但没有运气。

$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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"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)","Jax.Teller@Contoso.com.",,"Enabled","16.08.2016 15:50:27"

1 个答案:

答案 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!