在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塊的最後。 –