gpt4 book ai didi

powershell - (Powershell) 捕获 "Get-WinEvent : No events were found"Get-WinEvent

转载 作者:行者123 更新时间:2023-12-02 22:32:33 27 4
gpt4 key购买 nike

当我运行以下命令按 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";
}
}

关于powershell - (Powershell) 捕获 "Get-WinEvent : No events were found"Get-WinEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26680036/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com