none
Log Monitoring with specific keywords RRS feed

  • Question

  • Hi,

    We have three jobs (eg: firstjob_date) so when the first job gets triggered there is an entry made in logs folder which keeps on updating and once it completed the other job triggers automatically and same process starts.

    I have written a powershell script that we keep on checking and looking for specific words in logs ("1,0x" or  "0,0x") , but the thing here is - when the job is running for long or takes time the loop doesn't work is there any way we can hold it to find these two keywords in logs ("1,0x" or  "0,0x") ?

    $location = "D:\temp"
    $date = (get-date).adddays(-1).tostring("yyyyMMdd")
    $firstjob = "first" + "_" + $date
    $cond = test-path -path $location\firstjob* -erroraction silrentlycontinue
    if($cond -eq 'True')
    {
    write-host "the file is present forr today"
    $pattern1 = "error" , "1,0x" , "0,0x"
    foreach($word in $pattern1)
    {
    $op1 = get-content -path $location\firstjob* |where-object {$_ -match $word}
    write-host $op1
    }
    }
    else
    {
    write-host "the file is not generated"
    }

    $secondjob = "second" + "_" + $date
    $cond = test-path -path $location\secondjob* -erroraction silrentlycontinue
    if($cond -eq 'True')
    {
    write-host "the file is present forr today"
    $pattern2 = "error" , "1,0x" , "0,0x"
    foreach($word in $pattern2)
    {
    $op2 = get-content -path $location\secondjob* |where-object {$_ -match $word}
    write-host $op2
    }
    }
    else
    {
    write-host "the file is not generated"
    }

    • Moved by Bill_Stewart Thursday, December 20, 2018 9:21 PM This is not "debug/fix/rewrite my script for me" forum
    Saturday, October 20, 2018 6:03 PM

All replies

  • No loops needed.

    Get-Content -path $location\firstjob*  | Select-String $pattern

    This will match all words in the pattern,


    \_(ツ)_/

    Saturday, October 20, 2018 6:21 PM
  • hi,

    here is the thing when the job is running the loop will search for keyword like error or ("1,0x" or  "0,0x") and if it not finding , it is coming out , i need this for loop to keep to searching until it finds ("1,0x" or  "0,0x") - because the end of line consists ("1,0x" or  "0,0x") , i need the script to keep on searching for it

    What changes i can make?


    • Edited by rv007sh Saturday, October 20, 2018 6:53 PM
    Saturday, October 20, 2018 6:40 PM
  • There is no need for a loop.  The code I posted will search all files for all keywords and return them.

    Lets start by you fixing your original post and posting the code using the tool provided and properly indenting the code so it is readable.


    \_(ツ)_/

    Saturday, October 20, 2018 7:01 PM
  • Your code is quite confusing and doesn't appear to make much sense.

    Here is what your code looks like when all unnecessary items are removed and it is formatted correctlu.

    $location = 'D:\temp'
    $date = (get-date).adddays(-1).tostring('yyyyMMdd')
    $firstjob = 'first' + '_' + $date
    $secondjob = "second" + "_" + $date
    $pattern1 = 'error' , '1,0x' , '0,0x'
    $pattern2 = "error" , "1,0x" , "0,0x"
    
    if(test-path -path $location\firstjob*){
        write-host 'the file is present forr today'
        Get-Content -path $location\firstjob*  | Select-String $pattern1
    }else{
        write-host "the file is not generated"
    }     
    
    if(test-path -path $location\secondjob*){
        write-host "the file is present forr today" 
        Get-Content -path $location\secondjob*  | Select-String $pattern2
    }else{
        write-host "the file is not generated"
    }

    Why are you using a wildcard? 

    This will return all lines that match any word in the collection of patterns.


    \_(ツ)_/

    Saturday, October 20, 2018 7:14 PM