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?
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)
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 )
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With