Repository navigation
Description
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
SteveL-MSFT commentedon Nov 15, 2016
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 usels ~in PowerShell, butget-childitem ~will return file and directory objects instead of just text for further processing.kedziorm commentedon Nov 16, 2016
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
lsis 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"?
[-]PermissionDenied - Access to home directory is denied on Linux[/-][+]Bad error message when using Get-Content with a Directory[/+]SteveL-MSFT commentedon Nov 16, 2016
Mateusz (@kedziorm) agree that the error message isn't a good one.
Updated title to reflect this discussion
iSazonov commentedon Oct 8, 2017
Should we silently skip directory objects as
Unblock-Filebecause we can get paths from pipeline?SteveL-MSFT commentedon Oct 8, 2017
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 ideaSteveL-MSFT commentedon Nov 1, 2017
@PowerShell/powershell-committee reviewed this and agreed that we should have a better error message and not silently ignore directories
zhenggu commentedon Sep 18, 2018
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?
SteveL-MSFT commentedon Sep 18, 2018
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-Contentis called on a folder, it should just error out early with a good error message than catch an exception from CoreFx.