我正在使用PowerShell工作,在控制台中运行一个包含以下代码的脚本:
$inpath = "C:\users\xxxxx\path\foo\bar"
我一直遇到这个错误:
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
essException
+ FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand
这些脚本和目标文件都位于我的本地驱动器上。我可以在资源管理器中访问这些文件,使用记事本查看/编辑/保存它们,并且没有设置任何权限限制。当我使用命令行时,可以在我的路径上成功运行get-content
cmdlet来处理文件。我可以更改目录 PS C:> cd C:\users\xxxxx\path\foo\bar
并成功列出其中的内容。更有趣的是,我可以复制脚本中出现错误的那一行,并在命令行上不会收到错误提示。
PS C:\users\xxxxx\path\foo> $inpath = "C:\users\xxxxx\path\foo\bar"
PS C:\users\xxxxx\path\foo>
这让我怀疑“权限被拒绝”错误实际上是其他什么问题,或者是足够模糊,以至于我不知道如何进行故障排除。PS是否可能具有与其运行的用户不同的权限?有人见过这种行为吗?你是如何解决这个问题的?我相信有一个我不知道的简单解决方案。