【问题标题】:Monitor log in real time for any of two specific lines实时监控两条特定线路中的任何一条的日志
【发布时间】:2022-10-01 00:18:27
【问题描述】:

所以我有一个日志文件,它会得到一行包含“部署失败”或“部署成功”的行(它可以是“30092002 - 部署失败”),然后做一些事情。

我想出了这个:

$keywords=Get-Content \"keywords.txt\"
Get-Content \"the_log.log\" -tail 1 -wait |
     ForEach-Object{
          foreach($word in $keywords){
               if($_ -contains $word){
                   echo \"deployment completed\"
               }
          }
     }

关键字的内容。文本

deployment failed
deployment successful

然后我在日志中添加了一个新行(只是为了模拟实时追加)。 它按预期工作。

但是,当我向文件中添加类似下面的内容时,它无法识别并且回显不起作用:

30092002-[XASE1353QEF] - deployment failed for something

如何使这些关键字像通配符一样,即使它们的左侧或右侧有东西也能检测到它们并继续执行脚本的其余部分?

    标签: powershell


    【解决方案1】:

    我认为 -match (regex) 会有所帮助,例如:

    #Read your list of keywords here
    $keywords = @("keyWord1","keyWord2","keyWord3")
    #We join the elements of the array with | so we can use it as a regex pattern
    $keywordsString = $keywords -join "|"
    Get-Content [path] -tail 1 -wait | %{
        If ($_ -match "$keywordsString"){
            write-host "deployment completed"
        }
    }
    

    【讨论】:

    • 感谢您的建议,刚刚尝试过,不幸的是不起作用。不返回任何结果。
    • 它起作用了...一旦我将一个关键字添加到文件中,我就会得到“部署完成”,也许你很快并且确实抓住了我发布的第一个版本我做错了?
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多