除了来自DBCC的警告之外,powershell正则表达式错误

时间:2014-04-15 14:24:43

标签: sql-server regex powershell regex-negation

我正在尝试创建powershell脚本,该脚本将通过某些Sql Server脚本的日志文件并调查单词“error”的存在。我做到了:

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

或     获取内容$ 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)"

但我不清楚正则表达式的模式。

2 个答案:

答案 0 :(得分:1)

尝试使用Where-Object过滤掉与您要排除的文字相匹配的行。

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

答案 1 :(得分:1)

让我知道这是否有帮助:

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

-NotMatch参数负责不匹配DBCC打印的错误消息。