PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

Get-Content -Wait fails on WSL2 files

Open Josephur opened this issue 1 year ago • 1 comments

Prerequisites

Steps to reproduce

Map your WSL isntance to a drive letter. For instance my W: drive is mapped to \wsl$\Ubuntu

Run Get-Content with the -Path and -Wait parameter to print out the text file and monitor it for changes: Example: Get-Content -Path "W:\home\joseph\myvoipms\cron.log" -Wait

Expected behavior

Get-Content should wait for new content in the file, and print it as it comes in.

Actual behavior

Get-Content prints out the file, but then says "Get-Content: Incorrect function."

Error details

Exception             :
    Type            : System.ComponentModel.Win32Exception
    NativeErrorCode : 1
    ErrorCode       : -2147467259
    TargetSite      :
        Name          : WaitForChanges
        DeclaringType : [Microsoft.PowerShell.Commands.FileSystemContentReaderWriter]
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message         : Incorrect function.
    Source          : System.Management.Automation
    HResult         : -2147467259
    StackTrace      :
   at Microsoft.PowerShell.Commands.FileSystemContentReaderWriter.WaitForChanges(String filePath, FileMode fileMode,
FileAccess fileAccess, FileShare fileShare, Encoding fileEncoding)
   at Microsoft.PowerShell.Commands.FileSystemContentReaderWriter.ReadByLine(Boolean waitChanges, List`1 blocks,
Boolean readBackward)
   at Microsoft.PowerShell.Commands.FileSystemContentReaderWriter.Read(Int64 readCount)
   at Microsoft.PowerShell.Commands.GetContentCommand.ProcessRecord()
CategoryInfo          : InvalidOperation: (:) [Get-Content], Win32Exception
FullyQualifiedErrorId : ProviderContentReadError,Microsoft.PowerShell.Commands.GetContentCommand
InvocationInfo        :
    MyCommand        : Get-Content
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 3
    Line             : Get-Content -Path "W:\home\josephur\myvoipms\cron.log" -Wait
    Statement        : Get-Content -Path "W:\home\josephur\myvoipms\cron.log" -Wait
    PositionMessage  : At line:1 char:1
                       + Get-Content -Path "W:\home\josephur\myvoipms\cron.log" -Wait
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Get-Content
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
      0
      1

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.3
PSEdition                      Core
GitCommitId                    7.4.3
OS                             Microsoft Windows 10.0.26100
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

Josephur avatar Jun 19 '24 06:06 Josephur

Does Linux PowerShell running within WSL happily wait on the same file?

What file system type does the drive appear as to Windows?

Get-Content

Wait is a dynamic parameter that the FileSystem provider adds to the Get-Content cmdlet. This parameter works only in file system drives. Wait can't be combined with Raw.

FileSystemContentStream.cs

WaitForChanges uses System.IO.FileSystemWatcher

Remote computers must have one of the required platforms installed for the component to function properly.

I don't see a list of required platforms for this function

Can you replace this with

wsl --user joseph tail -f myvoipms/cron.log

rhubarb-geek-nz avatar Jun 21 '24 05:06 rhubarb-geek-nz

@Josephur does this still repro for you in latest Windows? Members of the cmdlet WG hasn't been able to repro this and works as expected

SteveL-MSFT avatar Nov 06 '24 17:11 SteveL-MSFT

This issue still exists for me using the latest PowerShell 7.4.6

Here is a video of the bug.

Get-Content should with the -Wait switch latch to the file and continue to read it, I wonder if it's something to do with the mapped drive to WSL perhaps?

Josephur avatar Nov 14 '24 00:11 Josephur

Oh and to answer your question, if I install Powershell within my Ubuntu WSL, and use the same command on the same file, it works fine. It's only when Windows is using the mapped drive to access the Linux files when the error occurs.

Here's the details of the mapped drive:

C:\Users\Josephur>net use
New connections will be remembered.
Status       Local     Remote                    Network
-------------------------------------------------------------------------------
             W:        \\wsl$\Ubuntu             Plan 9 Network Provider

Josephur avatar Nov 17 '24 07:11 Josephur

This looks like an issue in the WSL file handler, I would suggest opening an issue in the WSL repo with a PowerShell 5.1 repro

SteveL-MSFT avatar Dec 18 '24 17:12 SteveL-MSFT

This issue has been marked as external and has not had any activity for 1 day. It has been be closed for housekeeping purposes.

📣 Hey @Josephur, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback