2014-04-15 26 views
0

我想創建PowerShell腳本,將通過一些Sql Server腳本的日誌文件,並調查存在的單詞「錯誤」。我這樣做:PowerShell正則表達式錯誤,除了從DBCC警告

Get-Content $log | Select-String "error" -quiet #returns true/false 

或 Get-Content $ log |選擇字符串「錯誤」 #returns線與「錯誤」

問題是,我的日誌文件中還包含來自DBCC類似的警告:

26: DBCC execution completed. If DBCC printed error messages, contact your system administrator. 
24: DBCC execution completed. If DBCC printed error messages, contact your system administrator. 

如何列出只是「錯誤」行,但不「如果DBCC打印錯誤消息」? 我嘗試了諸如:

Get-Content "d:\InsightMasterESF\logs\currentlog.txt" | Select-String -pattern "(error)(?!If DBCC printed error messages)" 

但我不知道正則表達式的模式很好。

+1

正則表達式的第一條規則是「知道你的數據」。如果您需要正則表達式的幫助,我們需要確切知道我們需要匹配的數據是什麼樣的。 – mjolinor

回答

1

嘗試使用Where-Object過濾出與您要排除的文本相匹配的行。

Get-Content -Path $Log | Where-Object -FilterScript { $PSItem -match 'error' -and $PSItem -notmatch 'If DBCC printed'; }; 
+0

直到你發佈它,我纔會給出相同的答案。可能希望將-notmatch'DBCC'更改爲更具體一些,並將其設置爲-notmatch'如果DBCC打印',以便它不會超出期望的排除範圍。 – TheMadTechnician

+0

@TheMadTechnician:完成:)謝謝 –

1

讓我知道,如果這有助於:

Select-String -Path "d:\InsightMasterESF\logs\currentlog.txt" -Pattern error | select-string -Pattern 'If DBCC printed error messages' -NotMatch -Quiet 

-notmatch參數需要不匹配DBCC輸出了錯誤消息的照顧。

+0

它的工作。謝謝。 –