当我运行下面的命令按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";
}
}请帮帮忙。谢谢
发布于 2014-11-01 01:20:29
这是一个非终止性错误,不会被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";
}
}https://stackoverflow.com/questions/26680036
复制相似问题