2013-06-19 48 views
2

在交互模式下,這個工程:如何設置Powershell的地方,對象過濾事件日誌

Get-Eventlog -log application -after ((get-date).addMinutes(-360)) -EntryType Error 

現在我想篩選出某些消息,下面沒有過濾所期望的字:

Get-Eventlog -log application -after ((get-date).addMinutes(-360)) -EntryType Error | where-object {$_.$Message -notlike "*Monitis*"} 

另外,我該如何在多個條件上放置對象?

在-and聲明

在我的劇本,我得到錯誤:

$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 

錯誤:

-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 ... 
+          ~~~~ 
+0

今天,第一部分似乎正在工作(-notlike)。當我昨天運行它時,可能還有其他時間問題或者-after子句的問題。 – NealWalters

+0

顯然,你不能在每個之間放一個換行符?如果我把所有的話和陳述都放在一行上,它似乎就行得通。但是這使代碼很難讀取/維護。我最終可能會有10條語句。也許我應該只使用連續的管道? – NealWalters

回答

4

在你的第二個代碼片段對「消息」之前刪除美元符號。閱讀如下。如果您使用的是PowerShell ISE,則會看到「消息」應該是黑色而不是紅色。

Get-Eventlog -log application -after ((get-date).addMinutes(-360)) -EntryType Error | where-object {$_.Message -notlike "*Monitis*"} 

對於第三代碼段,我開始在位置對象濾波器換行符之前放置一個grave accent。這告訴PowerShell你正在繼續一條線,而不是開始一條新線。此外,在PowerShell ISE中,比較運算符( - 和& - 不喜歡)從藍色和黑色變爲灰色。

$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 
+1

嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖嘖道事實在的嘖嘖嘖嘖!啊,我現在看到額外的$標誌,嗯......謝謝 – NealWalters

0

日期簡化: ((get-date).addMinutes($minutes*-1))((get-date).addMinutes(-1)) 相同的輸出和 (get-date).addMinutes(-1)

相同的輸出此外,我覺得addDays(-1)更加有用。

+0

好評,但不應該發佈爲答案 – rob

+0

「你必須有50個評論的聲望」我想我的理由是把它作爲答案 - 我想我本可以等待。 –

相關問題