【问题标题】:Catching Get-EventLog errors捕获 Get-EventLog 错误
【发布时间】:2015-06-17 21:54:04
【问题描述】:

我正在编写一个简单的脚本来解析一些事件日志,但是当没有结果或 instanceid 无效时,我需要消除一些错误:

PS C:\> get-eventlog Application -instanceid 1111

Get-EventLog : No matches found
At line:1 char:13
  + get-eventlog <<<<  Application -instanceid 1111
      + CategoryInfo          : ObjectNotFound: (:) [Get-EventLog], ArgumentException
      + FullyQualifiedErrorId : GetEventLogNoEntriesFound,Microsoft.PowerShell.Commands.GetEventLogCommand

我可以这样做并使其静音,但这也会使其他错误静音:

PS C:\> try { get-eventlog Application -instanceid 1111 -erroraction stop } catch { }

我试过了,但它不起作用:

PS C:\> try { get-eventlog Application -instanceid 1111 -erroraction stop } catch [ObjectNotFound] { }

Unable to find type [ObjectNotFound]: make sure that the assembly containing this type is loaded.
At line:1 char:91
  + try { get-eventlog Application -instanceid 1111 -erroraction stop } catch [ObjectNotFound] <<<< { }
     + CategoryInfo          : InvalidOperation: (ObjectNotFound:String) [], RuntimeException
     + FullyQualifiedErrorId : TypeNotFound

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以使用-ErrorAction SilentlyContinue 并在其后检查您的$error 变量,

    $error[0] 
    

    它将始终包含最后一个错误对象。

    【讨论】:

      【解决方案2】:

      绝不是唯一的选择,但您可以尝试以下方法:

      $result = get-eventlog Application -instanceid 1111 -erroraction silentlycontinue
      if($result){
          Write-Host "Found some."
      } else{
          Write-Host "wah wah wah waaaah... you know like the trombone sound"
      }
      

      我再一次没有完全阅读帖子。为了更好地回答我的问题,我提供了这个可能会帮助您的try 阻止问题

      try {
          get-eventlog Application -instanceid 1111 -ErrorAction Stop
      } Catch [Exception]{
          $theError = $_
          Switch($theError .Exception.GetType().FullName){
              System.InvalidOperationException{Write-Host "This happened: $($theError.Exception.Message)"}
              System.ArgumentException {Write-Host "This happened: $($theError.Exception.Message)"}
              default{"Something else happened: $($theError.Exception.GetType().FullName)"}
          }
      }
      

      使用-Stop 创建一个终止错误。捕获任何异常并将错误对象放入变量中,以便以后可以在其他范围内使用。获取异常名称并在其上使用 switch 语句来确定适当的操作。在您的“未找到匹配项”的情况下,它会抛出 [System.ArgumentException],您可以通过查看 $_.Exception.GetType().FullName 的值来判断。捕获 switch 语句中的特定错误,如果您尚未捕获特定异常,则可以在default 中查看详细信息。

      当我将 cmdlet 调用中的“应用程序”替换为“Fizgig”时,[System.InvalidOperationException] 的价值发生了

      【讨论】:

      • 问题是我希望显示其他错误,例如日志文件错误或无法建立连接。我只想让无效的 instanceid 和空结果静音。
      【解决方案3】:

      您应该尝试以下语法以在不停止脚本执行的同时获取错误:

          try
          {
              # check for eventLog
              Get-EventLog -LogName "Application" -InstanceId 1111 -ErrorAction Stop
          }
          catch
          {
              # send error as ID
              Write-Warning "Error -Message $($_.Exception.Message) -Line $($_.InvocationInfo.ScriptLineNumber) -Time $(Get-Date -Format 'HH.mm.ss.fff')"
          }
      

      【讨论】:

        猜你喜欢
        • 2019-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多