9

I have a PowerShell script that dumps a gazillion lines to a csv file while processing a very large XML.

During the while read of the xml reader I write each line to a csv file like so:

  $newline | add-content -path $csv_file

This works for 99% but occasionally I see in the log "add-content: stream was not readable" for 1 or 2 items out of gazillions, I presume because it is busy writing the past line to it.

Any resolution?

edelwater
  • 2,650
  • 8
  • 39
  • 67

2 Answers2

12

Its an old post, but I run into a similar problem (using Set-Content instead of Add-Content). In my case, the WriteAllText Method solved that issue.

This should solve it for you:

[System.IO.File]::AppendAllText($csv_file,$newline)
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
4

Here is a PowerShell approach if you don't want to call the windows API. Put in a try catch block within a do loop to retry until successful.

    $isWritten = $false

    do {
        try {
            Add-Content -Path $csv_file -Value $newline -ErrorAction Stop
            $isWritten = $true
        }
        catch {
        }
    } until ( $isWritten )
}
Jim
  • 692
  • 7
  • 15
  • This is 5 years later and im totally outside of that project long ago, but it looks like something i will add to all entries where add-content is used. Maybe even add max 5 retries. Maybe by default wrap Add-Content. – edelwater Feb 02 '20 at 20:42