在Powershell中,它好像总是会执行Finally块。如果Catch块 已被执行,是否有办法告诉Powershell在Finally块中不执行语句。示例我的下面的代码,如果电子邮件已被触发,那么我不希望powershell生成日志文件。Powershell - 错误处理(如果Catch块已被调用,不要调用finally块)
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Send-MailMessage -From xxxxxxxxxx
Break
}
Finally
{
$Time=(Get-Date).ToString('MM/dd/yyyy hh:mm:ss tt')
"This script made a read attempt at" + "$d1" | out-file $logfile -append
}
这与“终于”的概念是相反的。这听起来像你的finally块中应该在你的try块的最后。 –