抑制错误消息

时间:2012-10-30 10:46:07

标签: powershell powershell-v2.0

当我运行以下并且没有.csv文件时我得到一个错误,说明路径为空 - 这是因为没有什么可以删除...它不会停止脚本但我得到用户的思考有一些错误,因为powershell喜欢展示RED角色并且让人们感到害怕!

如何让脚本运行但是抑制错误?

CODE:

filter removeallcsv([string]$path = '.')
    {
        Get-ChildItem $path | Where-Object {$_.Extension -eq ".csv"} | Foreach-Object {$_.fullName}
    }


Remove-Item (removeallcsv)

2 个答案:

答案 0 :(得分:1)

您可以使用If声明:

$files = removeallcsv

if ($files) { Remove-Item $files } else {"nothing to delete"}

一个班轮:

 if ( ($files=removeallcsv)) { Remove-Item $files } else {"nothing to delete"}

答案 1 :(得分:1)

你实际上是将$ null传递给Remove-Item,试试这个:

Get-ChildItem $path | Where-Object {$_.Extension -eq '.csv'} | Remove-Item