Skip to content

Bad error message when using Get-Content with a Directory #2686

Closed

Description

@kedziorm

I just installed the latest release of the Powershell by typing:
sudo dpkg -i powershell_6.0.0-alpha.12-1ubuntu1.16.04.1_amd64.deb

There was no errors during installation, but it seems that I have no access to Linux paths from Powershell (even as a root).

Steps to reproduce

Run powershell and type:
Get-Content ~

Expected behavior

Should provide content of the home directory of current user (similarly as Linux command ls ~).

Please note that even if I run powershell as root (by typing sudo powershell) I receive mentioned error.

Actual behavior

"PermissionDenied" error

powershell

PS /home/testingdata> Get-Content ~                                             
Get-Content : Access to the path '/home/testingdata' is denied.
At line:1 char:1
+ Get-Content ~
+ ~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (/home/testingdata:String) [Ge 
   t-Content], UnauthorizedAccessException
    + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsof 
   t.PowerShell.Commands.GetContentCommand
 
PS /home/testingdata> whoami                                                    
testingdata
PS /home/testingdata> 

Trying as a root:

sudo su
cd ~

PS /root>  Get-Content ~                                                        
Get-Content : Access to the path '/root' is denied.
At line:1 char:1
+ Get-Content ~
+ ~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (/root:String) [Get-Content],  
   UnauthorizedAccessException
    + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsof 
   t.PowerShell.Commands.GetContentCommand
 
PS /root> whoami                                                                
root

Environment data

I use Ubuntu 16.04 LTS 64-bit.

Name Value


PSVersion 6.0.0-alpha
PSEdition Core
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 3.0.0.0
GitCommitId v6.0.0-alpha.12
CLRVersion
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Activity

  1. added
    Resolution-By Design
    on Nov 15, 2016
  2. SteveL-MSFT commented on Nov 15, 2016

    @SteveL-MSFT
    Member

    Get-Content is for reading the contents of a file. ~ resolves to the home directory which is not a file. The PowerShell cmdlet you should be using is Get-ChildItem:

    get-childitem ~

    this is equivalent to ls ~ although you can just use ls ~ in PowerShell, but get-childitem ~ will return file and directory objects instead of just text for further processing.

  3. kedziorm commented on Nov 16, 2016

    @kedziorm
    Author

    Steve Lee (@SteveL-MSFT) Sorry, shame on me.
    I'm really sorry for such stupid mistake (I used both cmdlets in past...) - I should check ls since ls is a standard alias for Get-ChildItem.
    Okay, so it's obvious that it's my fault, but I think that error message might be confusing, suggesting that it is permission issue - maybe that should be "Unable to read content of... because ... is directory, file expected"?

  4. added
    WG-Cmdlets
    and removed
    Resolution-By Design
    on Nov 16, 2016
  5. changed the title [-]PermissionDenied - Access to home directory is denied on Linux[/-] [+]Bad error message when using Get-Content with a Directory[/+] on Nov 16, 2016
  6. added
    Up-for-Grabs
    on Nov 16, 2016
  7. SteveL-MSFT commented on Nov 16, 2016

    @SteveL-MSFT
    Member

    Mateusz (@kedziorm) agree that the error message isn't a good one.

    > cat ./Downloads/
    /bin/cat: ./Downloads/: Is a directory
    

    Updated title to reflect this discussion

  8. iSazonov commented on Oct 8, 2017

    @iSazonov
    Collaborator

    Should we silently skip directory objects as Unblock-File because we can get paths from pipeline?

  9. SteveL-MSFT commented on Oct 8, 2017

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) that's an interesting idea and makes sense to me as an improvement, but since it's a break change, let's have Committee review the idea

  10. added
    Review - Committee
    on Oct 8, 2017
  11. SteveL-MSFT commented on Nov 1, 2017

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this and agreed that we should have a better error message and not silently ignore directories

  12. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Nov 1, 2017
  13. zhenggu commented on Sep 18, 2018

    @zhenggu
    Contributor

    The bug is caused by class FileStream of dotnet raise System.UnauthorizedAccessException when passes path with one directory, should we fix in function CreateStreams of FileSystemContentStream.cs, or submit a bug to dotnet?

  14. SteveL-MSFT commented on Sep 18, 2018

    @SteveL-MSFT
    Member

    Zheng Gu (@zhenggu) I don't think dotnet will or should make a change as in that case an app is explicitly (although accidentally) trying to read a directory as a FileStream. I think this fix will be in the FileSystemProvider where if Get-Content is called on a folder, it should just error out early with a good error message than catch an exception from CoreFx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedResolution-FixedUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions