0

I have a script that is using Add-Content to log progress. The log files are written to a network share, and on some networks I have gotten stream not readable errors. Rare, but often enough to want to address the issue. I found [this thread][1] that seems to offer an answer. And I initially implemented this loop

$isWritten = $false

    do {
        try {
            Add-Content -Path $csv_file -Value $newline -ErrorAction Stop
            $isWritten = $true
        }
        catch {
        }
    } until ( $isWritten )

but I added a 1 second wait between tries, and limited myself to 20 tries. I figured no network could be such crap that it would timeout for longer than that. But on one network I still have problems, so I bumped the count to 60, and STILL have failures to write to the log. I tried [System.IO.File]::AppendAllText($file, $line) and that seems to solve all the timeouts, at least in 20 some odd tries it hasn't failed, where before I would get 1 or two failures in 10 tries. But the formatting is off, likely I need to set the encoding. But more importantly, I wonder what is actually the SOURCE of the issue in Add-Content, and why does [System.IO.File]::AppendAllText() not have the issue, and is this a sign of potentially other problems with the network, or with the machines at this one location? Or just a bug in PowerShell that I need to work around. FWIW, it's PS 5.1 on Windows 10 21H2. Also, FWIW, the logs can get to a few hundred lines long, but I often see the error in the first 10 lines. [1]: add-content produces stream not readable

Gordon
  • 6,257
  • 6
  • 36
  • 89
  • If you want to use a specific Encoding you can target the [`AppendAllText(String, String, Encoding)` overload](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.appendalltext?view=net-6.0#system-io-file-appendalltext(system-string-system-string-system-text-encoding)). As for answering why would `Add-Content` fail, it's pretty hard to answer that cmdlets code is proprietary on WinPS – Santiago Squarzon Jul 12 '22 at 14:05
  • Well, I want to use the encoding option, but I can't seem to make it work. NotePad++ says my existing logs using Add-Content are UTF8, but when I use `[System.Text.Encoding]::UTF8` it's obvious the CRLF stuff is wrong, and the resulting logs are unreadable. – Gordon Jul 12 '22 at 14:11
  • Right, `[System.Text.Encoding]::UTF8` is UTF8 no-BOM. Try using `[System.Text.UTF8Encoding]::new()` – Santiago Squarzon Jul 12 '22 at 14:13
  • So, I tried `[System.IO.File]AppendAllText($path, $value, ([System.Text.UTFEncoding]::new()))` and that freezes and never writes anything to console or file. Tried making a variable for the encoding too. Do I actually need an argument in that `::new()`? – Gordon Jul 12 '22 at 14:19
  • 1
    You're missing an `8` after `UTF`. It works properly for me. Also, are you sure you want to use `AppendAllText` and not `AppendAllLines` ? – Santiago Squarzon Jul 12 '22 at 14:24
  • Doh, that was stupid. Fixed that but the logs are still a mess. But I suspect it's where I am building multi-line log entries and I will need to start handling CRLF differently. – Gordon Jul 12 '22 at 14:29
  • 1
    Which is why I asked if you're sure you want to use `AppendAllText` and not `AppendAllLines` – Santiago Squarzon Jul 12 '22 at 14:31
  • 1
    Ah, I got the 8 added and it worked, and didn't come back to catch the rest of the response. I did now, and interestingly AppendAllLines freezes again. But AppendAllText with "$Value`r`n" as the second argument seems to work. I'll bang on AppendAllLines a bit more and see if I can figure out why that isn't working. Thanks! – Gordon Jul 12 '22 at 14:39
  • It may be possible that `AppendAllLines` is not working because if you attempt to add a single string it will give you an error, the argument requires it to be `IEnumerable` so, something like `[IO.File]::AppendAllLines($myFile, [string[]] 'myTest', $encoding)` should work properly. Note the need to use `[string[]]` for a single string – Santiago Squarzon Jul 12 '22 at 15:56
  • Interesting. I will give that a try too. What is odd is that AppendAllLines is working on meh Dev machine, but not in a production situation. But I have been in the process of refactoring to generic lists, which may impact things. But now I have info to troubleshoot with, so that will help. Weird Add-Content behavior to start The Who situation, but more and more I find myself using PowerShell as glue to hold together a lot of .NET method calls rather than native cmdlets. Just seems to work better/faster. – Gordon Jul 12 '22 at 16:07
  • 1
    I personally love .NET APIs in general and will put them first over native cmdlets when there is a performance improvement or something that can't be done with native cmdlets, but that's just personal preference. Also worth noting, if you have the chance to install the newer versions of PS Core, test your code with them. .NET Core improves everything and at least if you still have an error with `Add-Content` the code is open source – Santiago Squarzon Jul 12 '22 at 16:12
  • 1
    Unfortunately, moving to Core is not really an option. My audience is architects, and lots of them would still be on Windows 7 and PS2.0 if Autodesk would let them. :) – Gordon Jul 12 '22 at 16:15

0 Answers0