windows Remove-Item 不起作用,Delete 起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25606481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove-Item doesn't work, Delete does
提问by Lieven Keersmaekers
Does anyone have any idea why Remove-Item
would fail while Delete
works?
有谁知道为什么Remove-Item
在Delete
工作时会失败?
In below script, I get a list of files I'd like to delete.
Using Remove-Item
I get following error message:
在下面的脚本中,我得到了要删除的文件列表。
使用Remove-Item
我收到以下错误消息:
VERBOSE: Performing the operation "Remove File" on target "\\UncPath\Folder\test.rtf". Remove-Item : Cannot remove item \\UncPath\Folder\test.rtf: Access to the path is denied.
详细:在目标“\\UncPath\Folder\test.rtf”上执行“删除文件”操作。删除项目:无法删除项目 \\UncPath\Folder\test.rtf:对路径的访问被拒绝。
but using Delete
is deleting those files as we speak.
但Delete
在我们说话时使用正在删除这些文件。
Script
脚本
$files = gci \UncPath\Folder| ?{ $_.LastWriteTime -le (Get-Date).addDays(-28) }
# This doesn't work
$files | Remove-Item -force -verbose
# But this does
$files | % { $_.Delete() }
采纳答案by Keith Hill
I can finally repro this and IMO it appears to be a bug. The repro is to have an open share like C$ but to set Deny Modify perms for the user on the file. When I do that, I observe this:
我终于可以重现这个和 IMO 它似乎是一个错误。重现是有一个像 C$ 这样的开放共享,但在文件上为用户设置拒绝修改权限。当我这样做时,我观察到:
PS> gci '\Keith-PC\C$\Users\Keith\foo.txt' | ri -for
ri : Cannot remove item \Keith-PC\C$\Users\Keith\foo.txt: Access to the path is denied.
At line:1 char:43
+ gci '\Keith-PC\C$\Users\Keith\foo.txt' | ri -for
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (\Keith-PC\C$\Users\Keith\foo.txt:FileInfo) [Remove-Item], ArgumentExc
eption
+ FullyQualifiedErrorId : RemoveFileSystemItemArgumentError,Microsoft.PowerShell.Commands.RemoveItemCommand
PS> gci '\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Delete()} # <== this works!
I also observe that removing the -Force
parameter deletes the file without error as well. The deny perms still allow me to delete the file from Windows Explorer so that leads me to believe that the file should delete. So what is up with using the -Force
parameter? When I delve into the ErrorRecord I see this:
我还观察到删除-Force
参数也会删除文件而不会出错。拒绝权限仍然允许我从 Windows 资源管理器中删除文件,这让我相信该文件应该删除。那么使用-Force
参数有什么用呢?当我深入研究 ErrorRecord 时,我看到:
Message : Access to the path is denied.
ParamName :
Data : {}
InnerException :
TargetSite : Void set_Attributes(System.IO.FileAttributes)
StackTrace : at System.IO.FileSystemInfo.set_Attributes(FileAttributes value)
at Microsoft.PowerShell.Commands.FileSystemProvider.RemoveFileSystemItem(FileSystemInfo
fileSystemInfo, Boolean force)
It seems that the -Force
parameter is trying to set (more likely reset) attributes and the permissions on the file don't allow it e.g.:
似乎该-Force
参数正在尝试设置(更可能是reset)属性并且文件的权限不允许它,例如:
PS> gci '\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Attributes = 'Normal'}
Exception setting "Attributes": "Access to the path is denied."
At line:1 char:45
+ gci '\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Attributes = 'Normal'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
So it seems to me that PowerShell should first try as if the -Force
weren't present and if that fails, then try resetting attributes.
所以在我看来,PowerShell 应该首先尝试,就好像-Force
不存在一样,如果失败,然后尝试重置属性。
回答by Lo?c MICHEL
powershell may act strange with UNC path, I think it prepends the UNC Path with the current provider you can verify this with :
powershell 可能对 UNC 路径表现得很奇怪,我认为它在 UNC 路径前面加上当前提供程序,您可以使用以下方法进行验证:
cd c:
test-path \127.0.0.1\c$
returns TRUE
返回真
cd HKCU:
test-path \127.0.0.1\c$
returns FALSE
返回 FALSE
when specifying the fullpath we're telling powershell to use the filesystem provider, that solves the problem. you could also specify the provider like remove-item filesystem::\\uncpath\folder
在指定完整路径时,我们告诉 powershell 使用文件系统提供程序,这解决了问题。您还可以指定提供者,例如remove-item filesystem::\\uncpath\folder