PowerShell foreach loop export to new column in CSV format

坚强是说给别人听的谎言 提交于 2019-12-12 01:25:06

问题


I am currently working with a CSV file that has a manager's employee number, but not their SAMAccountName. I want to utilize Get-ADUser to grab the manager's SAMAccountName from their EmployeeNumber attribute and place that inside a new column in the same CSV file.

CSV sample:

"Full Name","Username","Manager","Manager User Sys ID"
"User 1","u1","1, Manager","123456"
"User 2","u2","2, Manager","234567"
"User 3","u3","3, Manager","345678"

I would like:

"Full Name","Username","Manager","Manager User Sys ID","Manager SamAccountName"
"User 1","u1","1, Manager","123456","m1"
"User 2","u2","2, Manager","234567","m2"
"User 3","u3","3, Manager","345678","m3"

I have spent some time putting together the following code. I can get a new column added and can further grab the SAMAccountName, but it only exports a single line in the new CSV file like this:

"SAMAccountName","managerUsername"
"m1","@{SAMAccountName=m1}"

Here is the code:

$managers = Import-Csv -Path .\test.csv
$usermananger = @()
foreach ($manager in $managers)
{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($manager."Manager User Sys ID")" -Properties SAMAccountName |
    Select-Object SAMAccountName
  $exportstring = $testmanager |
    Select-Object *,@{Name='managerUsername';Expression={"$getmanagersam"}}
  $exportstring | Export-Csv -Path .\simpletest.csv -NoTypeInformation
}

回答1:


As @MathiasR.Jessen mentioned in the comments: you need to expand the SamAccountName property to get just the value. Also, you're overwriting your output CSV with every iteration. Either append to the file, or move the Export-Csv cmdlet outside the loop. The former requires PowerShell v3 or newer, the latter requires that you change the loop to a ForEach-Object loop (or run the foreach loop in a subexpression).

Personally I'd prefer using a pipeline, so I'd pick the latter:

Import-Csv -Path .\test.csv | ForEach-Object {
  $acct = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" |
          select -Expand SamAccountName
  $_ | select *,@{Name='managerUsername';Expression={$acct}}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation



回答2:


The short answer is to add the -Append option to your export-csv statement to stop it overwriting each time round the loop.

Alternatively move the export outside the loop as follows:

$managers = Import-Csv -Path .\test.csv

$managers|foreach-object{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" | select -ExpandProperty SAMAccountName
  $_|Select *,@{Name='managerUsername';Expression=$getmanagersam}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation

Note: looks like @AnsgarWiechers beat me to it :-)




回答3:


I'm late to the party it looks like, but that rarely stops me... I'm not a huge fan of adding properties via the Select cmdlet, so I'm going to offer an alternative where you pipe the CSV to a ForEach-Object loop (just like the other answers), but inside that you use Add-Member with the -PassThru argument, and then pipe it off to the output file. I will add a new line and indent after the pipes, just to make it easier to read.

Import-Csv -Path ".\test.csv" | 
    ForEach-Object {Add-Member -InputObject $_ -NotePropertyName 'managerUserName' -NotePropertyValue (Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')").samaccountname -PassThru} |
    Export-Csv -Path ".\simpletest.csv" -NoTypeInformation

This should essentially do the exact same thing as the other two answers, it just adds the property differently, because variety is the spice of life! (I saw that on the internet somewhere, so it must be true.)



来源:https://stackoverflow.com/questions/29973404/powershell-foreach-loop-export-to-new-column-in-csv-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!