【问题标题】:(Powershell) Catch "Get-WinEvent : No events were found" Get-WinEvent(Powershell) 捕获“Get-WinEvent:未找到事件”Get-WinEvent
【发布时间】:2014-10-31 17:12:00
【问题描述】:

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

请帮忙。谢谢

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-ise


    【解决方案1】:

    这是一个非终止错误,不会被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";
                     }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-25
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多