PowerShell 中的权限错误
我是 PowerShell 新手。当尝试编写一个简单的脚本来删除文件夹的内容,然后用从不同文件夹复制的文件填充它时,我总是收到 PermissionDenied
错误。
详情:
+ remove-item <<<< D:\path\* -recurse
+ CategoryInfo : PermissionDenied: (save.gif:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
问题出在哪里?我可以通过资源管理器操作这两个文件夹。 从脚本文件和 shell(使用 Windows PowerShell ISE)运行时都会发生该错误。 ISE 进程在我的帐户下运行。 我运行的是 Windows 7 Professional,并且是本地管理员。
编辑: 经过Richard的建议,我尝试了详细模式(似乎没有效果)。
PS Z:\> $error[0] | fl * -force
PSMessageDetails :
Exception : System.IO.IOException: Not Enough permission to perform operation.
TargetObject : D:\path\file.txt
CategoryInfo : PermissionDenied: (D:\path\file.txt:FileInfo) [Remove-Item], IOException
FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
ErrorDetails : Cannot remove item D:\path\file.txt: Not Enough permission to perform operation.
InvocationInfo : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {0, 1}
我在那里没有看到任何有多大用处(但无论如何感谢您的提示)。
编辑2: 好的,这是脚本源:
remove-item D:\path_A\* -recurse
copy-item D:\path_B\* D:\path_A\
就是这样。删除项目似乎会抛出每个文件。
I am new to PowerShell. When trying to write a simple script that deletes the contents of a folder and then fills it with files copied from a different folder, I always get a PermissionDenied
error.
Details:
+ remove-item <<<< D:\path\* -recurse
+ CategoryInfo : PermissionDenied: (save.gif:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Where is the problem? I am able to manipulate both folders through Explorer.
The error occurs both when running from a script file and from shell (using Windows PowerShell ISE).
The ISE process runs under my account.
I'm running Windows 7 Professional and am a local administrator.
Edit:
After Richard's suggestion, I tried the verbose mode (which seemed to have no effect).
PS Z:\> $error[0] | fl * -force
PSMessageDetails :
Exception : System.IO.IOException: Not Enough permission to perform operation.
TargetObject : D:\path\file.txt
CategoryInfo : PermissionDenied: (D:\path\file.txt:FileInfo) [Remove-Item], IOException
FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
ErrorDetails : Cannot remove item D:\path\file.txt: Not Enough permission to perform operation.
InvocationInfo : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {0, 1}
I don't see anything of much use there (but thanks for the tips anyway).
Edit 2:
Okay, here's the script source:
remove-item D:\path_A\* -recurse
copy-item D:\path_B\* D:\path_A\
That's it. The remove-item seems to throw at every file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你尝试过吗:
Have you try :
是否启用UAC?如果是这样,请尝试以“管理员”身份运行 PowerShell 会话。您确实没有删除对象的权限。
我工作的地方有非常严格的安全策略,不熟悉 UAC 的用户总是会受到困扰。
Is UAC enabled? If so try running your PowerShell session as 'Administrator'. It really looks like you don't have permission to delete the objects.
We have very restrictive security policies where I work and users not familiar with UAC get burned all the time.
除了上述帖子中提到的原因之外,我还观察到,当文件被单独的进程访问时,会引发“访问被拒绝”错误(在我的例子中,我必须在运行 Rename-Item 之前停止服务器)成功地)。
In addition to reasons mentioned in the above posts, i've observed that "Access Denied" error is thrown when the file is being accessed by a separate process (In my case, i had to stop the server before Rename-Item could be run successfully).
在此错误之后(并假设此时是最近的错误):
将展开错误和异常的详细信息。这应该会给你更多的信息。
另一件要做的事情是打开详细日志记录,
以获取有关错误发生时具体执行的操作的更多详细信息。
最后,PowerShell ISE 包含一个调试器,允许您单步执行脚本。
After this error (and assuming at that point in it the most recent error):
will expand the details of the error and exception. That should give you more information.
Another thing to do is to switch on verbose logging
to get more details about specifically what operation is being performed when the error occurs.
Finally PowerShell ISE includes a debugger which allows you to step through your script.