Powershell - Export-CSV and Append

后端 未结 4 701
心在旅途
心在旅途 2020-12-11 16:36

I have a script such as the following:

$in_file = \"C:\\Data\\Need-Info.csv\"
$out_file = \"C:\\Data\\Need-Info_Updated.csv\"
$list = Import-Csv $in_file 
Fo         


        
相关标签:
4条回答
  • 2020-12-11 17:12

    Convert the foreach loop into a foreach-object and move the export-csv to outside the outter foreach object so that you can pipe all the objects to the export-csv.

    Something like this (untested):

    $list | ForEach {
    $zID = $_.zID
    ForEach-Object { Get-QADUser -Service 'domain.local' -SearchRoot 'OU=Users,DC=domain,DC=local' -SizeLimit 75000 -LdapFilter "(&(objectCategory=person)(objectClass=user)(PersonzID=$zID))" |
    Select-Object DisplayName,samAccountName,@{Name="zID";expression={$zID}} | 
    
    }
    } |Export-Csv $out_file -NoTypeInformation -Force
    
    0 讨论(0)
  • 2020-12-11 17:13

    -append is broken in Powershell v2.0 Use Dmitry Sotikovs workaound: http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/

    I would however recommend manojlds excellent solution!

    0 讨论(0)
  • 2020-12-11 17:18

    As Sune mentioned, PowerShell v3's Export-Csv has an Append flag but no character encoding protection. manojlds is correct, since your code is writing all new data to a new CSV file.

    Meanwhile, you can append data to a CSV by:

    1. Convert the objects to CSV with ConvertTo-Csv
    2. Strip the header —and type information if necessary— and collect the CSV data only
    3. Append the new CSV data to the CSV file through Add-Content or Out-File, be sure to use same character encoding

    Here is a sample:

    1..3 | ForEach-Object {
     New-Object PSObject -Property @{Number = $_; Cubed = $_ * $_ * $_}
    } | Export-Csv -Path .\NumTest.csv -NoTypeInformation -Encoding UTF8
    
    # create new data
    $newData = 4..5 | ForEach-Object {
     New-Object PSObject -Property @{Number = $_; Cubed = $_ * $_ * $_}
    } | ConvertTo-Csv -NoTypeInformation
    
    # strip header (1st element) by assigning it to Null and collect new data
    $null, $justData = $newData
    
    # append just the new data
    Add-Content -Path .\NumTest.csv -Value $justData -Encoding UTF8
    
    # create more new data, strip header and collect just data
    $null, $data = 6..9 | ForEach-Object {
     New-Object PSObject -Property @{Number = $_; Cubed = $_ * $_ * $_}
    } | ConvertTo-Csv -NoTypeInformation
    
    # append the new data
    Add-Content -Path .\NumTest.csv -Value $data -Encoding UTF8
    
    # verify
    Import-Csv .\NumTest.csv
    
    # clean up
    Remove-Item .\NumTest.csv
    
    0 讨论(0)
  • 2020-12-11 17:18

    After version 3 you can use:

    Export-Csv $out_file -append -notypeinformation -encoding "unicode"
    
    0 讨论(0)
提交回复
热议问题