我終於可以重現這個和IMO似乎是一個錯誤。 repro是有一個像C $這樣的開放共享,但是爲該文件上的用戶設置Deny Modify perms。當我這樣做,我觀察此:
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!
我還觀察到,去除-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)
看來,-Force
參數嘗試設置(更可能復位)屬性和文件的權限不允許它例如:
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
因此,在我看來,PowerShell應該首先嚐試,如果-Force
不存在,如果失敗,然後嘗試重置屬性。
在我的PSV4盒子上工作。你正在運行什麼版本?你是否嘗試指定完整路徑:'$ files = gci \\ UncPath \ Folder | ?{$ _。LastWriteTime -le(Get-Date).addDays(-28)| select fullname}' – 2014-09-01 13:12:01
'$ PSVersionTable'爲'PSVersion'返回4.0。追加'select fullname',我現在得到一個像這樣的'Remove-Item:找不到路徑'的錯誤消息\\ UncPath \ Folder \ @ {FullName = \ UncPath \ Folder \ test.RTF}',因爲它不存在。 。 – 2014-09-01 13:16:47
'$ files [0]''返回'\\ UncPath \ Folder \ test.RTF' – 2014-09-01 13:18:38