您不能单独使用该 cmdlet 执行此操作。您必须为错误提供额外的逻辑。
'D:\temp\abc.txt', 'D:\Temp\hw.txt', 'D:\Temp\nonexistent.txt.', 'D:\Temp\book1.csv' |
ForEach{
try {Remove-Item -Path $PSitem -WhatIf -ErrorAction Stop}
catch {Write-Warning -Message $PSItem.Exception.Message}
}
# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
What if: Performing the operation "Remove File" on target "D:\Temp\hw.txt".
WARNING: Cannot find path 'D:\Temp\nonexistent.txt.' because it does not exist.
What if: Performing the operation "Remove File" on target "D:\Temp\book1.csv".
#>
您应该在所有代码(交互式和脚本)中利用错误处理。
您可以将任何屏幕输出发送到 $null、Out-Null 或 [void] 以防止它进入屏幕,但仍然知道它发生了。
对于您要求的用例,您将需要多个逻辑(try/catch、if/then)语句。
所以,类似这个修改过的包装函数:
function Remove-ItemNotFileLocked
{
[cmdletbinding(SupportsShouldProcess)]
[Alias('rinf')]
param
(
[parameter(Mandatory = $true)][string]$FullFilePath
)
$TargetFile = New-Object System.IO.FileInfo $FullFilePath
if ((Test-Path -Path $FullFilePath) -eq $false) {return $false}
try
{
$TargetFileStream = $TargetFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($TargetFileStream)
{
$TargetFileStream.Close()
Remove-Item -Path $FullFilePath
}
$false
}
catch
{
$true
}
}
'D:\temp\abc.txt', 'D:\Documents\Return To Sender.docx','D:\Temp\nonexistent.txt.' |
ForEach {Remove-ItemNotFileLocked -FullFilePath $PSItem -WhatIf}
# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
True
False
#>
注意点:记事本等文本编辑器不会锁定文件。
- 第一条消息显示一个记事本文件已打开,但可以删除
- 第二个显示 Word 文档打开和锁定
- 第三个显示一个文本文件,不在系统上
如果我不想要那种屏幕噪音,那么...
注释掉那些 $false 和 $True 语句,它们用于调试和验证工作。
'D:\temp\abc.txt', 'D:\Documents\Return To Sender.docx','D:\Temp\nonexistent.txt.' |
ForEach {$null = Remove-ItemNotFileLocked -FullFilePath $PSItem -WhatIf}
# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
#>
当然,删除/注释掉 -WhatIf 以允许事情发生,这种噪音也会消失。
如果您不想使用某个函数,那么此代码块应该可以解决您的用例。
# Remove non-Locked file and show screen output
'D:\temp\abc.txt', 'D:\Documents\Return To Sender.docx','D:\Temp\nonexistent.txt.' |
ForEach{
try
{
$TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::ReadWrite,
[System.IO.FileShare]::None
)
$TargetFile.Close()
Remove-Item -Path $PSItem -WhatIf
}
catch [System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
catch {$PSItem.Exception.Message}
}
# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
Exception calling "Open" with "3" argument(s): "The process cannot access the file 'D:\Documents\Return To Sender.docx' because it is being used by another process."
Exception calling "Open" with "3" argument(s): "Could not find file 'D:\Temp\nonexistent.txt'."
#>
# Remove non-Locked file and silence screen output
'D:\temp\abc.txt', 'D:\Documents\Return To Sender.docx','D:\Temp\nonexistent.txt.' |
ForEach{
try
{
$TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::ReadWrite,
[System.IO.FileShare]::None
)
$TargetFile.Close()
Remove-Item -Path $PSItem -WhatIf
}
catch [System.Management.Automation.ItemNotFoundException]{$null = $PSItem.Exception.Message}
catch {$null = $PSItem.Exception.Message}
}
# Results
<#
What if: Performing the operation "Remove File" on target "D:\temp\abc.txt".
#>