Open SimonESHT opened 3 weeks ago
Could you do a single pipeline rather than continually opening and closing?
Also, does setting $ErrorActionPreference at the root make any difference?
$destfile = "\\UNCSHARE\Folder\Export_Sample_$(get-date -Format "yyyyMMdd_HHmmss").txt"
$srcFile = '\\UNCSHARE\Folder\200MB_Export_20231208_1545.txt'
[int64]$ln = 0
[int64]$SampleCount = 0
$ErrorActionPreference = 'Stop'
Get-Content -LiteralPath $srcFile | ForEach-Object {
$SampleCount++
$ln++
if ($ln -eq 1 -or $ln % 500 -eq 0)
{
$_
}
} | Set-Content -LiteralPath $destFile
write-host $SampleCount
(get-content $destfile).count
Thanks, Yes, I found that to be an effective workaround. See the Stack Overflow thread [here]https://stackoverflow.com/questions/78378535/powershell-out-file-append-silently-skips-a-few-rows-when-used-inside-foreach-l The point of raising this ticket though was to alert that the Out-File cmdlet is not raising a terminating error when it cannot open the file for write access. The expected behaviour (as seen in PS5.1 with the native API call) is that it should as I illustrated above
Exception calling "AppendAllText" with "2" argument(s): "The process cannot access the file
'\\UNCSHARE\Export_sample20240424_164810.txt' because it is being used
by another process."
To be clear, I have found an alternative approach, but the behaviour of Out-File in PS Core, when writing to UNC network stream, does not make it apparent that any error has occurred.
I did try adding $ErrorActionPreference ="STOP"
at the root of the script but it still didn't report an error.
Stack OverflowI am trying to extract a representative sample of a 200MB csv file by writing the header and every 500th row to a new file for testers to use. My first attempt was knowingly sub-optimal but seemed ...
Prerequisites
Steps to reproduce
I am trying to extract a representative sample of a 200MB csv file by writing the header and every 500th row to a new file for testers to use. I relied on out-file -append to add each row matching the modulus condition to the destination file on network share, but what I found is that I had slightly fewer rows in the sample file than expected. Repeated runs produced slightly different numbers of rows in the destination file (expected 2014, actual ranged between 1992-2011). I did not get an terminating error.
The behaviour does NOT occur if I use a location on my local hard drive for the destination file.
I tried a suggested alternative of switching to the native API call:
That still doesn't produce a terminating error in Powershell 7.4.2, but does in Windows Powershell 5.1
Out-File to a UNC path doesn't produce a terminating error in Powershell 7.4.2 or 5.1.
Expected behavior
Actual behavior
Error details
No response
Environment data
Visuals
No response