【问题标题】:Powershell Get-eventlog query. How to return X number of entries that meet criteriaPowershell Get-eventlog 查询。如何返回 X 个符合条件的条目
【发布时间】:2014-05-08 02:18:59
【问题描述】:

我想查找多个服务器的某个事件 ID 列表的最近一次出现。我没有看到一个很好的方法来做到这一点。如果我使用-newest 开关,我必须根据每个服务器事件日志的相对大小以及我感兴趣的事件在该数量的条目中发生的机会来调整数字。在下面的示例中,服务器 F6WINMSSTEST3 没有我在前 10,000 个条目中寻找的内容。有谁知道这样做的好方法吗?

我想要为每个服务器列出我正在寻找的事件的每个 ID 的最新条目的单个实例,以便我可以看到它们何时发生。在理想情况下,每台服务器都会列出最近的 3 个 ID。

$Servers = "F6WINMSSTEST","F6WINMSSTEST2","F6WINMSSTEST3","F6WINMSSTEST4","F6WINMSSTEST5" 

Foreach ($server in $Servers) {

$server

get-eventlog -computer $server -logname system -newest 10000 | where-object { $_.eventid -   eq 6005 -or  $_.eventid -eq 6009 -or  $_.eventid -eq 6006} } 

样本输出:

F6WINMSSTEST

   Index Time          EntryType   Source                 InstanceID Message                                                                                                                               
   ----- ----          ---------   ------                 ---------- -------                                                                                                                               
  108265 Feb 08 08:33  Information EventLog               2147489653 The Event log service was started.                                                                                                    
  108264 Feb 08 08:33  Information EventLog               2147489657 Microsoft (R) Windows (R) 6.01. 7601 Service Pack 1 Multiprocessor Free.                                                              
  108247 Feb 08 08:31  Information EventLog               2147489654 The Event log service was stopped.                                                                                                    
  104703 Nov 16 08:41  Information EventLog               2147489653 The Event log service was started.                                                                                                    
  104702 Nov 16 08:41  Information EventLog               2147489657 Microsoft (R) Windows (R) 6.01. 7601 Service Pack 1 Multiprocessor Free.                                                              
  104688 Nov 16 08:39  Information EventLog               2147489654 The Event log service was stopped.                                                                                                    
F6WINMSSTEST2
   39265 Jul 06 08:01  Information EventLog               2147489653 The Event log service was started.                                                                                                    
   39264 Jul 06 08:01  Information EventLog               2147489657 Microsoft (R) Windows (R) 6.00. 6002 Service Pack 2 Multiprocessor Free.                                                              
   39249 Jul 06 08:00  Information EventLog               2147489654 The Event log service was stopped.                                                                                                    
   39060 Jul 06 02:03  Information EventLog               2147489653 The Event log service was started.                                                                                                    
   39059 Jul 06 02:03  Information EventLog               2147489657 Microsoft (R) Windows (R) 6.00. 6002 Service Pack 2 Multiprocessor Free.                                                              
   39044 Jul 06 02:02  Information EventLog               2147489654 The Event log service was stopped.                                                                                                    
F6WINMSSTEST3
F6WINMSSTEST4
    6591 Jul 06 08:01  Information EventLog               2147489653 The Event log service was started.                                                                                                    
    6590 Jul 06 08:01  Information EventLog               2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Uniprocessor Free.                                                                
    6589 Jul 06 08:00  Information EventLog               2147489654 The Event log service was stopped.                                                                                                    
    6531 Jul 05 11:52  Information EventLog               2147489653 The Event log service was started.                                                                                                    
    6530 Jul 05 11:52  Information EventLog               2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Uniprocessor Free.                                                                
    6529 Jul 05 11:51  Information EventLog               2147489654 The Event log service was stopped.                                                                                                    
F6WINMSSTEST5
   55124 Nov 06 19:11  Information EventLog               2147489653 The Event log service was started.                                                                                                    
   55123 Nov 06 19:11  Information EventLog               2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Uniprocessor Free.                                                                
   55122 Nov 06 19:10  Information EventLog               2147489654 The Event log service was stopped.  

【问题讨论】:

    标签: powershell get-eventlog


    【解决方案1】:

    我写了几次然后删除了这篇文章,现在我想我知道发生了什么。

    • 写入事件日志条目的任何内容都会写入一个名为“EventID”的字段
    • 这并不真正包含 EventID,它实际上在值的高位包含一些额外的数据
    • EventViewer / PowerShell / etc. 去除高位并将结果显示为 EventId
    • 它们将原始值显示为 InstanceId,它可能与 EventID 匹配,但可能不匹配。

    这会导致以下情况:

    • 两个完全不同的事件可能具有相同的 EventID,它可能会发生冲突。您还必须检查“来源”是否是您想要的。
    • 通过 PowerShell 获取 EventID 很慢,获取 InstanceId 很快,因为它已编入索引。

    因此,对于您的问题,如果您可以获取其中每个事件之一,然后获取 InstanceID,那么您可以向 Get-EventLog 询问您关心的事件的 InstanceId,然后使用 -newest 1

    试试:

    $Servers = "F6WINMSSTEST","F6WINMSSTEST2","F6WINMSSTEST3","F6WINMSSTEST4","F6WINMSSTEST5" 
    
    ForEach ($server in $Servers) {
        Write-Output $server
        Get-EventLog -computer $server -LogName System -InstanceId ?,?,? -Newest 1 
    }
    

    当您找到实例 ID 时。

    指定-Source 也可能是个好主意。

    否则这个讨论:http://social.technet.microsoft.com/Forums/scriptcenter/en-US/616b67ee-9e71-4f23-abb8-5c88e8890b9e/event-logs-relationship-between-instanceid-and-eventid?forum=ITCG 是我从上面得到的,是和你有同样问题的人,他们评论:

    Get-WinEvent cmdlet 接受 -FilterXML 参数,您可以在其中 指定事件 ID。所以这解决了上层的问题 机器,但对于低级机器(我希望有更好的方法 说“2000-XP-2K3”/“Vista-7-2008”)我们仍然需要过滤 事后,如果你明白我的意思。

    如果您可以选择应该更快的 InstanceId,但我希望看到一个权威参考,说明它稳定可靠,或者不能冲突,或类似的。

    【讨论】:

      【解决方案2】:

      在我的查询中,退货一直是最新到最旧的。这使得这个命令只带回最新的:

      get-eventlog  -logname system  | where ((eventid -eq 6005) -or (eventid -eq 6006) -or (eventid -eq 6009)) | select -first 1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多