2014-09-01 151 views
3

有沒有人有任何想法爲什麼Remove-Item會失敗,而Delete的作品?Remove-Item不起作用,Delete做


在下面的腳本中,我得到了一個我想刪除的文件列表。
使用Remove-Item我獲得以下錯誤信息:

VERBOSE:執行操作目標 「刪除文件」 「\\ UncPath \文件夾\ TEST.RTF」。刪除項目:無法刪除項目 \\ UncPath \ Folder \ test.rtf:對路徑的訪問被拒絕。

但是使用Delete正在刪除這些文件。

腳本

$files = gci \\UncPath\Folder| ?{ $_.LastWriteTime -le (Get-Date).addDays(-28) } 

# This doesn't work 
$files | Remove-Item -force -verbose 

# But this does 
$files | % { $_.Delete() } 
+0

在我的PSV4盒子上工作。你正在運行什麼版本?你是否嘗試指定完整路徑:'$ files = gci \\ UncPath \ Folder | ?{$ _。LastWriteTime -le(Get-Date).addDays(-28)| select fullname}' – 2014-09-01 13:12:01

+0

'$ PSVersionTable'爲'PSVersion'返回4.0。追加'select fullname',我現在得到一個像這樣的'Remove-Item:找不到路徑'的錯誤消息\\ UncPath \ Folder \ @ {FullName = \ UncPath \ Folder \ test.RTF}',因爲它不存在。 。 – 2014-09-01 13:16:47

+0

'$ files [0]''返回'\\ UncPath \ Folder \ test.RTF' – 2014-09-01 13:18:38

回答

2

我終於可以重現這個和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不存在,如果失敗,然後嘗試重置屬性。

+0

這很有道理。 Kayasax解決了我眼前的問題*(tx再次)*,但未來的遊客從這個被接受的答案中獲益更多。我改變了接受的答案。 – 2014-09-02 17:05:55

+1

我提交了這個缺陷。如果您同意,請對其進行表決:https://connect.microsoft.com/PowerShell/feedbackdetail/view/962980/remove-item-force-fails-when-remove-item-succeeds-on-the-same-file – 2014-09-07 22:29:37

7

PowerShell中可以充當奇怪的與UNC路徑,我認爲它預先考慮與當前提供商的UNC路徑,你可以驗證這一點:

cd c: 
test-path \\127.0.0.1\c$ 

返回TRUE

cd HKCU: 
test-path \\127.0.0.1\c$ 

返回false

指定FULLPATH當我們告訴PowerShell來使用文件系統提供商,解決該問題。你也可以指定供應商喜歡remove-item filesystem::\\uncpath\folder

+0

許多thx的解決方案和解釋 – 2014-09-01 15:39:43

+0

嗯,仍然不確定這解釋了什麼OP是看到的 - 我不能複製。 「訪問被拒絕」的錯誤意味着'$ files | ri'正在查找該文件,但由於燙髮無法刪除。當我使用只讀分享時,我會看到這一點。如果我像上面那樣使用管理員共享(C $),那麼remove-item會成功。實際上,在管道中傳遞給Remote-Item的LiteralPath參數的是完整的提供程序路徑「PSPath」(LiteralPath具有別名PSPath - 因此是管道綁定)。你上面演示的是一個非驅動器限定路徑'\\\'作爲參數是有問題的。 :-) – 2014-09-01 22:45:26

+0

@凱特希爾 - fyi,你可能是對的。額外的測試表明,通過刪除'-force'參數,我的初始腳本也可以工作。 – 2014-09-02 05:11:35