当我运行以下命令按ID列出日志时,它说Get-WinEvent : No events were found that match the specified selection criteria.
如何捕获此异常并显示一条简单消息“未找到事件”。
我执行的命令
Get-WinEvent -FilterHashtable @{LogName="Application";ID="191"}
我在下面尝试了以下方法,但无法使其正常工作-
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";
}
}
请帮忙。谢谢
最佳答案
这是一个不会终止的错误,不会被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";
}
}