首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置Powershell where-object来过滤EventLog

如何设置Powershell where-object来过滤EventLog
EN

Stack Overflow用户
提问于 2013-06-19 23:24:43
回答 2查看 13K关注 0票数 2

在交互模式下,这是有效的:

代码语言:javascript
复制
Get-Eventlog -log application -after ((get-date).addMinutes(-360)) -EntryType Error

现在我想要过滤掉某些消息,下面的消息没有过滤所需的单词:

代码语言:javascript
复制
Get-Eventlog -log application -after ((get-date).addMinutes(-360)) -EntryType Error | where-object  {$_.$Message -notlike "*Monitis*"}

另外,我如何在where-object上加入多个条件?

在我的脚本中,我得到了关于-and语句的错误:

代码语言:javascript
复制
$getEventLog = Get-Eventlog -log application -after ((get-date).addMinutes($minutes*-1)) -EntryType Error 
# list of events to exclude 
$getEventLogFiltered = $getEventLog | where-object {$_.Message -notlike "Monitis*" 
                                       -and $_.Message -notlike "*MQQueueDepthMonitor.exe*"
                                       }
$tableFragment = $getEventLogFiltered | ConvertTo-Html -fragment

错误:

代码语言:javascript
复制
-and : The term '-and' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\scripts\EventLogExtract2.ps1:24 char:40
+                                        -and $_.Message -notlike "*MQQueueDepthMo ...
+                                        ~~~~
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-26 00:29:12

在你的第二个代码片段中,去掉"Message“前的美元符号。读起来像下面这样。如果您使用的是PowerShell ISE,您将看到"Message“应为黑色而不是红色。

代码语言:javascript
复制
Get-Eventlog -log application -after ((get-date).addMinutes(-360)) -EntryType Error | where-object  {$_.Message -notlike "*Monitis*"}

对于第三个代码片段,我在Where-Object过滤器中开始换行之前放置了一个grave accent。这会告诉PowerShell您是在继续一行,而不是开始一个新行。此外,在PowerShell ISE中,比较运算符(-and和-notlike)从蓝色和黑色变为灰色。

代码语言:javascript
复制
$getEventLog = Get-Eventlog -log application -after ((get-date).addMinutes($minutes*-1)) -EntryType Error 
# list of events to exclude 
$getEventLogFiltered = $getEventLog | where-object {$_.Message -notlike "Monitis*" `
                                       -and $_.Message -notlike "*MQQueueDepthMonitor.exe*"
                                       }
$tableFragment = $getEventLogFiltered | ConvertTo-Html -fragment
票数 4
EN

Stack Overflow用户

发布于 2015-10-02 21:23:42

数据简化:((get-date).addMinutes($minutes*-1))具有与((get-date).addMinutes(-1))相同的输出和与(get-date).addMinutes(-1)相同的输出

另外,我发现addDays(-1)更有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17195184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档