【问题标题】:Powershell: How can I extract time from the message field of eventlog?Powershell:如何从事件日志的消息字段中提取时间?
【发布时间】:2014-10-02 09:27:48
【问题描述】:

我正在尝试通过 Powershell 中的Get-EventLog 获取 Windows Sever 2008 机器的意外关机时间。我可以通过搜索 EventID 为 6008 的事件并仅选择 message 来接近,但我需要在字段内解析以获取它发生的时间(而不是事件触发的时间)。

我尝试使用replacementstrings[x],但找不到如何指定要使用的字段 (messages) 并且无法获得结果。

get-eventlog -LogName System -ComputerName svr-name | Where-Object {$_.EventID -eq 6008 -AND $_.timegenerated -gt (get-date).adddays(-30)}| select message

产生这个:

Message
-------
The previous system shutdown at 3:35:32 AM on ‎7/‎29/‎2014 was unexpected.
The previous system shutdown at 3:40:06 PM on ‎7/‎10/‎2014 was unexpected.`

【问题讨论】:

    标签: powershell event-log get-eventlog


    【解决方案1】:

    使用正则表达式,您可以将其拉出。

    $Messages = (get-eventlog -LogName System -ComputerName svr-name | Where-Object {$_.EventID -eq 6008 -AND $_.timegenerated -gt (get-date).adddays(-30) }| select message)
    $Messages | ForEach-Object { 
        $Matched = $_.Message -match "([0-9]{1,2}:.*[0-9]{4})"
        if ($Matched) {
            Write-Output "System rebooted at $($Matches[1])" 
        }
    }
    

    可能有更好的方法,但我不知道是什么:)

    我的系统的示例输出

    System rebooted at 4:34:30 PM on ‎4/‎20/‎2014
    System rebooted at 1:48:38 PM on ‎1/‎21/‎2014
    System rebooted at 1:37:12 PM on ‎1/‎21/‎2014
    System rebooted at 1:22:01 PM on ‎1/‎21/‎2014
    System rebooted at 4:41:21 PM on ‎11/‎22/‎2013
    

    【讨论】:

      【解决方案2】:

      从远程主机检索所有事件并在本地计算机上过滤它们通常表现不佳,因为这样您通过网络传输大量不相关的事件,只是为了将它们丢弃。 Get-EventLog 具有按事件 ID 或源上给定时间戳之前/之后过滤消息的选项,因此最好使用这些选项来预先选择您真正感兴趣的消息。崩溃的时间戳可以从 @ 中提取带有正则表达式的 987654325@ 字段并通过 ParseExact() 解析为 DateTime 值:

      $log     = 'System'
      $server  = 'svr-name'
      $id      = [uint64]"0x80000000" + 6008
      $date    = (Get-Date).AddDays(-30)
      
      $fmt     = 'h:mm:ss tt on M\/d\/yyyy'
      $culture = [Globalization.CultureInfo]::InvariantCulture
      
      Get-EventLog -LogName $log -ComputerName $server -InstanceId $id -After $date | ? {
        $_.Message -match 'at (\d+:\d+:\d+ [ap]m on \d+/\d+/\d+) was unexpected'
      } | select MachineName, TimeGenerated,
                 @{n='Crashtime';e={[DateTime]::ParseExact($matches[1], $fmt, $culture)}}
      

      管道生成具有属性MachineNameTimeGeneratedCrashtime(最后一个是calculated property)的对象列表。如果您在变量中收集管道的输出(例如$evt),您可以像这样访问第三个对象的Crashtime 属性:

      $evt = .\script.ps1
      $evt[2].Crashtime
      

      【讨论】:

      • 我不知道InstanceID 过滤器。我发现EventID不一定对应InstanceID;在这种情况下 EventID 6008 = 2137489656 。我还是 Powershell 的新手,在第 12 行 (@...) 跟踪您的阵列数量时遇到了麻烦。如何调用此数组的元素(例如$Crashtime[2]?提前致谢。
      • @kiwisan:ITYM 2147489656(即 6008 + 0x80000000)。我更新了我的答案。
      【解决方案3】:

      更简单

      get-eventlog system  | where-object {$_.EventID -eq  "6008"} | fl
      

      【讨论】:

        猜你喜欢
        • 2018-08-31
        • 2020-01-09
        • 1970-01-01
        • 2018-07-19
        • 2022-06-17
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多