Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add-content produces stream not readable

Tags:

powershell

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?

like image 416
edelwater Avatar asked Dec 09 '14 02:12

edelwater


2 Answers

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)
like image 89
Martin Brandl Avatar answered Sep 21 '22 11:09

Martin Brandl


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 )
}
like image 31
Jim Avatar answered Sep 20 '22 11:09

Jim