by dwwilson66 at 2013-01-23 08:31:06
I’m working with PowerShell, running a script (from my console) that includes this line:by DonJ at 2013-01-23 08:51:58
$inpath = "C:\users\xxxxx\path\foo\bar"
I keep getting this error:
Get-Content : Access to the path ‘C:\users\xxxxx\path\foo\bar’ is denied.
At C:\users\xxxxx\path\foo\testscript.ps1:53 char:12
+ Get-Content <<<< $txtfile | Get-WordCount -Exclude (Get-Content c:\temp\exclude.txt) | select -First 15
+ CategoryInfo : PermissionDenied: (C:\users\xxxxx\path\foo\bar:String) [Get-Content], UnauthorizedAcc
+ FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand
The scripts and target files are all located on my local drive. I can access the files in Explorer, view/edit/save them using NotePad, and do not have any permissions restrictions set. When I’m on the command line, I can run the get-content cmdlet successfully on files in my path. I can change directories…
PS C:> cd C:\users\xxxxx\path\foo\bar…and successfully list what’s there. Even more interesting, I can duplicate the line that’s erroring in the script, and NOT receive an error on the command line.
PS C:\users\xxxxx\path\foo> $inpath = "C:\users\xxxxx\path\foo\bar"
This makes me suspect that the ‘Permission Denied’ error is actually something else, or something vague enough that I’ve got no clue how to proceed with troubleshooting. has anyone seen this behavior before, and how did you solve the problem? I’m sure there’s a simple solution that I don’t know.
It looks like you’re passing a folder to GC, not a file path. It can’t read folders.by nohandle at 2013-01-23 12:41:58
Nice catch Don. I tried it and I can confirm it behaves like that. Looking at the call stack it originates from FileStream. I don’t see any mention of that behavior reported on the MS connect site, would you mind checking it also, please?by DonJ at 2013-01-23 12:47:49
It’s not actually a bug. It’s “by design.” Keep in mind it is not “permission denied” which is what you interpreted. It’s “access denied.” As in, I can’t access the airplane because it isn’t there. “access” doesn’t always mean “permssion.” Plus, Connect won’t include Framework stuff. But this is a longstanding design thing in the Framework. One of those common gotchas. I usually mention it in class ;).by nohandle at 2013-01-23 13:11:36
Thanks Don that seems reasonable but the exception details mention both access denied and permissions denied:by DonJ at 2013-01-23 13:27:42
Exception : System.UnauthorizedAccessException: Access to the path ‘C:\temp’ is denied.
CategoryInfo : PermissionDenied: (C:\temp:String) [Get-Content], UnauthorizedAccessException
On first second and even third look I would still think the exception is related to me not having permissions to access the folder rather than the folder not being there.
Maybe after reading the official description of the exception class: “The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error.”, and ruling out all of the other alternatives.
Edit: I am pretty sure the reason for showing same error for access denied and file is not there is security
Yeh, welcome to the Frameworkby nohandle at 2013-01-23 13:37:50