How to export data to CSV in PowerShell?

后端 未结 4 1624
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 03:03
foreach ($computer in $computerlist) {
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {
        foreach ($file in $REMOVE) {
               


        
4条回答
  •  暖寄归人
    2020-12-03 03:41

    This solution creates a psobject and adds each object to an array, it then creates the csv by piping the contents of the array through Export-CSV.

    $results = @()
    foreach ($computer in $computerlist) {
        if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
        {
            foreach ($file in $REMOVE) {
                Remove-Item "\\$computer\$DESTINATION\$file" -Recurse
                Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\"            
            }
        } else {
    
            $details = @{            
                    Date             = get-date              
                    ComputerName     = $Computer                 
                    Destination      = $Destination 
            }                           
            $results += New-Object PSObject -Property $details  
        }
    }
    $results | export-csv -Path c:\temp\so.csv -NoTypeInformation
    

    If you pipe a string object to a csv you will get its length written to the csv, this is because these are properties of the string, See here for more information.

    This is why I create a new object first.

    Try the following:

    write-output "test" | convertto-csv -NoTypeInformation
    

    This will give you:

    "Length"
    "4"
    

    If you use the Get-Member on Write-Output as follows:

    write-output "test" | Get-Member -MemberType Property
    

    You will see that it has one property - 'length':

       TypeName: System.String
    
    Name   MemberType Definition
    ----   ---------- ----------
    Length Property   System.Int32 Length {get;}
    

    This is why Length will be written to the csv file.


    Update: Appending a CSV Not the most efficient way if the file gets large...

    $csvFileName = "c:\temp\so.csv"
    $results = @()
    if (Test-Path $csvFileName)
    {
        $results += Import-Csv -Path $csvFileName
    }
    foreach ($computer in $computerlist) {
        if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
        {
            foreach ($file in $REMOVE) {
                Remove-Item "\\$computer\$DESTINATION\$file" -Recurse
                Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\"            
            }
        } else {
    
            $details = @{            
                    Date             = get-date              
                    ComputerName     = $Computer                 
                    Destination      = $Destination 
            }                           
            $results += New-Object PSObject -Property $details  
        }
    }
    $results | export-csv -Path $csvFileName -NoTypeInformation
    

提交回复
热议问题