Home > Enterprise >  Monitor log in real time for any of two specific lines
Monitor log in real time for any of two specific lines

Time:10-01

So I have a log file which will either get a line containing "deployment failed" or "deployment successful" ( it can be "30092002 - deployment failed for something") and after that do something.

I came up with this:

$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"
               }
          }
     }

Content of keywords. txt

deployment failed
deployment successful

Then I have added a new line in the log ( just to simulate a realtime append). It is working as expected.

However, when I add to the file something like the below it is not recognized and the echo does not work:

30092002-[XASE1353QEF] - deployment failed for something

How to make these keywords be like a wild card and detect them even if there is something on left or right of them and continue with the execution of the remaining part of the script?

CodePudding user response:

Think this is all you need, as you said you only have 2 strings indicating a successfull or failed deployment and based on which one machtes you want to get a success or error message:

Get-Content [path] -tail 1 -wait | %{
    If ($_ -match "Single Sign On \[SSO\.zip\] deployment completed"){
        write-host "deployment completed"
    }
    ElseIf ($_ -match "Single Sign On \[SSO\.zip\] deployment failed"){
        write-error "deployment failed"
    }
}
  • Related