1

當我通過ID運行以下命令列出的日誌,它說:Get-WinEvent : No events were found that match the specified selection criteria.(PowerShell的)捕撈「GET-WinEvent:沒有發現事件」 GET-WinEvent

我怎麼能捕獲此異常,並顯示一條簡單的消息說「沒有發現事件」。我ran-

Get-WinEvent -FilterHashtable @{LogName="Application";ID="191"} 

命令我試過以下的,但不能讓它working-

try { Get-WinEvent -FilterHashtable @{LogName="Application";ID="191"} 
    } 

catch [Exception] { 
     if ($_.Exception -match "No events were found that match the specified selection criteria") { 
     Write-Host "No events found"; 
       } 
    } 

請幫助。謝謝

回答

2

這是一個無法終止的錯誤,它不會被try/catch捕獲。使用-ErrorAction Stop

try { Get-WinEvent -FilterHashtable @{LogName="Application";ID="191"} -ErrorAction Stop 
    } 

catch [Exception] { 
     if ($_.Exception -match "No events were found that match the specified selection criteria") { 
     Write-Host "No events found"; 
       } 
    } 
+0

謝謝! @briantist。有效 :) – 2014-10-31 17:28:49