简体   繁体   English

如何在Powershell中使用Select-String处理多种搜索模式

[英]How to handle multiple search patterns using Select-String in Powershell

I am parsing error log files across multiple servers and need to report on problem errors. 我正在解析多个服务器上的错误日志文件,并且需要报告问题错误。 The error log files should contain at least the following (known errors that are OK): 错误日志文件应至少包含以下内容(可以的已知错误):

  • ERROR Some error text 错误一些错误文本
  • ERROR Some more error text 错误更多错误文本
  • ERROR Here is more error text 错误这是更多错误文本

What I need is a Select-String search pattern that includes all three text strings above: 我需要的是一个选择字符串搜索模式,其中包括上述所有三个文本字符串:

$Search_Str_1 = "ERROR Some error text"
$Search_Str_2 = "ERROR Some more error text"
$Search_Str_3 = "ERROR Here is more error text"

$Search_Str_1, $Search_Str_2, and $Search_Str_3 are acceptable error strings and should not be reported if found in the error logs. $ Search_Str_1,$ Search_Str_2和$ Search_Str_3是可接受的错误字符串,如果在错误日志中找到,则不应报告。 However, if any additional error string is found that does not match any of the above search strings, then it should be reported as a bad error. 但是,如果找到与上述搜索字符串都不匹配的任何其他错误字符串,则应将其报告为严重错误。

Example: 例:

  • ERROR Some error text 错误一些错误文本
  • ERROR Some more error text 错误更多错误文本
  • ERROR Here is more error text 错误这是更多错误文本
  • ERROR This is bad error text and should be reported 错误这是错误的错误文字,应报告

The 4th error line (and any other line that does not match search_str 1, 2, or 3) would need to be reported as bad. 第4条错误行(以及与search_str 1、2或3不匹配的任何其他行)将需要报告为错误。

Hypothetical code: 假设代码:

$ErrorLog = Get-ChildItem -Path $LOG_PATH -Include Error.log -Recurse | Select-String -notmatch ($Search_Str_1 -or $Search_Str_2 -or $Search_Str_3)

How would I do this? 我该怎么做?

select-string -pattern takes in an array. select-string -pattern接受一个数组。 Put those search string in an array and pass it to the select string. 将这些搜索字符串放入数组中,然后将其传递给选择字符串。 Add the -NotMatch flag if you want it to not match the patterns you passed in. 如果希望它与您传入的模式不匹配,请添加-NotMatch标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM