PowerShell / PowerShell/PowerShell

Out-File not reporting error on failure to write to UNC network path when stream is being repeatedly opened and closed.

Ouverte
#21,545 2 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Needs-Triage
Langage dominant
C#
Étoiles
55.5k
Forks
8.5k
Merge moyen
1 j 2 h
PR mergées (30 j)
88

Description

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.

$destfile = "\\UNCSHARE\Folder\Export_Sample_$(get-date -Format "yyyyMMdd_HHmmss").txt"

$Original = get-content "\\UNCSHARE\Folder\200MB_Export_20231208_1545.txt"

[int64]$ln = 0
[int64]$SampleCount = 0

foreach ($line in $Original) {
    $ln++
    if ($ln -eq 1 -or $ln % 500 -eq 0) {
        $line | Out-File -FilePath $destfile -Append -ErrorAction Stop
        $SampleCount++
    }
}
write-host $SampleCount

(get-content $destfile).count 

I tried a suggested alternative of switching to the native API call:

#$line | Out-File -FilePath $destfile -Append -ErrorAction Stop -Encoding utf8
[System.IO.File]::AppendAllText($destfile, "$line`n")

That still doesn't produce a terminating error in Powershell 7.4.2, but does in Windows Powershell 5.1

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."

Out-File to a UNC path doesn't produce a terminating error in Powershell 7.4.2 or 5.1.

Expected behavior
For my example file. 
write-host $SampleCount
--2014
(get-content $destfile).count 
--2014
Actual behavior
For my example file. 
write-host $SampleCount
--2014
(get-content $destfile).count 
--a number ranging between 1995 and 2011 (depending on the number of times it failed to write a single line to the destination)
Error details

No response

Environment data
Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Visuals

No response

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Reproduisez les écritures répétées de Out-File -Append vers un chemin UNC sous Windows, en les comparant à une destination locale et à System.IO.File::AppendAllText. Suivez la manière dont Out-File gère l’ouverture et la fermeture du flux ainsi que les échecs d’écriture, puis vérifiez que les écritures ayant échoué sont signalées comme des erreurs et qu’aucune ligne d’exemple n’est perdue silencieusement.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
powershell
Domaine
cli
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.