【发布时间】: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