【问题标题】:Issue filtering out certain event logs from output问题从输出中过滤掉某些事件日志
【发布时间】:2015-06-16 23:35:50
【问题描述】:

所以我编写了一个小脚本,它通过一个漂亮的 HTML 电子邮件向我发送我的服务器的最近 24 小时事件日志,这样我就不必登录到每个服务器等。它运行良好,除了我们的一些应用程序产生我想过滤掉的大量事件。

我已经设法使用 where 语句来过滤掉一个,但是当我尝试向其中添加另一个事件 ID 时,它无法按预期工作。

我的代码片段如下:

$ApplicationEventsReport = @()
$ApplicationEvents = Get-EventLog -ComputerName $computer -LogName Application -EntryType Error,Warning -After (Get-Date).AddDays(-1) | where {$_.EventID -ne 1530}

foreach ($event in $ApplicationEvents) {
        $row = New-Object -Type PSObject -Property @{
                TimeGenerated = $event.TimeGenerated
                EntryType = $event.EntryType
                Source = $event.Source
                Message = $event.Message
                EventID = $event.InstanceID
        }
        $ApplicationEventsReport += $row
}

$ApplicationEventsReport = $ApplicationEventsReport | ConvertTo-Html -Fragment

您会注意到在 get-eventlog 行的末尾有 where 部分 -ne 是 ID,如果这有意义的话。

我尝试通过以下方式添加另一个:

where {$_.EventID -ne 1530 -or $_.EventID -ne 3221229571}

where {($_.EventID -ne 1530) -or ($_.EventID -ne 3221229571)}

where {$_.EventID -ne 1530} | where {$_.EventID -ne 3221229571}

where {$_.EventID -ne 1530} -or {$_.EventID -ne 3221229571}

但是这些都不起作用,他们要么完全忽略它,要么仍然只处理第一个(1530)......

任何关于为什么失败的想法,如果我在我的机器上本地运行该行,它们似乎都可以工作,一旦我放回脚本中它似乎只会倒下?

【问题讨论】:

  • 如果您只留下{$_.EventID -ne 3221229571} 会怎样——它本身会作为一个单独的条件起作用吗?
  • 那个较长的数字看起来像一个 InstanceID。 where {$_.EventID -ne 1530 -and $_.InstanceID -ne 3221229571} 工作?根据您尝试过滤的内容,创建数组并使用 -notcontains 可能是优先的。如果这些值不是针对同一个事件条目并且可能会阻止它们被排除,那么使用 -or 可能会给您带来麻烦。

标签: html powershell


【解决方案1】:

我想我看到了几个潜在的问题。如果我们从我的本地计算机上查看一些事件。

EventID InstanceId Message                                                                                                                                   
------- ---------- -------                                                                                                                                   
   1202 2147484850 Security policies were propagated with warning....                                                                                        
      0          0 The description for Event ID '0' in Source 'gupdate' cannot be found.  The local computer may not have the necessary registry informati...
   1001       1001 Fault bucket , type 0...                                                                                                                  
   1001       1001 Fault bucket , type 0...                                                                                                                  
   1003 1073742827 The Software Protection service has completed licensing status check....                                                                  
   1202 2147484850 Security policies were propagated with warning....                                                                                        
     32 1073741856 The store ...                                        
     32 1073741856 The store   ....

我看到了与您类似的数字,但对于 instanceID。所以一个简单的改变就是这样的。

Where-Object {$_.EventID -ne 1530 -and $_.InstanceID -ne 3221229571}

您应该使用-and,因为如果要过滤的记录仅匹配其中一个条件,使用 -or 可能会导致问题。

如果您有很多记录要过滤,那么您可以将这些值存储在数组中,以便在 Where 子句中使用。

$eventsToIgnore = "1004","1500","10001"
$instanceIDsToIgnore = "3221229571","2147484850"

#.... blah blah... other stuff

Where-Object {$eventsToIgnore -notcontains $_.EventID -and $instanceIDsToIgnore -notcontains $_.InstanceID}

【讨论】:

  • 完美的马特,我使用了数组方法并将其传送到 where 和 bingo!我想我也对 -or 和 -and 感到困惑,在我的脑海中 - 这意味着它需要同时具有两个值,但我猜它是 exclude 所以它是排除任何一个......这是漫长的一天!再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2015-03-28
  • 1970-01-01
相关资源
最近更新 更多