none
PowerShell - trying to execute if statement if text exists in log file RRS feed

  • Question

  • Here's a generic example of what I'm trying to do:  (I've tried both where-object and select-string, neither seem to give me the results I want)... so I'm guessing I'm missing something simple...

    $Errorinlog = Get-Content 'C:\file.log' | Where-Object { $_.Contains('error phrase') }

    if ($Errorinlog = 'error phrase')
    {
    echo exists

    Perform a function

    }

    Wednesday, March 2, 2016 2:55 PM

Answers

  • You can do the test directly in the if statement:

    If (Select-String -Path .\logFile2.txt -Pattern 'error phrase' -SimpleMatch) {
    
        Write-Host 'An error was found in the log' -ForegroundColor Red
    
    } Else {
    
        Write-Host 'No error detected' -ForegroundColor Green
    
    }
    
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> cat .\logFile1.txt
    garbage 1
    error phrase
    garbage 3
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> cat .\logFile2.txt
    garbage 1
    no error
    garbage 3
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> If (Select-String -Path .\logFile1.txt -Pattern 'error phrase' -SimpleMatch) {
    
        Write-Host 'An error was found in the log' -ForegroundColor Red
    
    } Else {
    
        Write-Host 'No error detected' -ForegroundColor Green
    
    }
    An error was found in the log
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> If (Select-String -Path .\logFile2.txt -Pattern 'error phrase' -SimpleMatch) {
    
        Write-Host 'An error was found in the log' -ForegroundColor Red
    
    } Else {
    
        Write-Host 'No error detected' -ForegroundColor Green
    
    }
    No error detected


    • Marked as answer by JoeFri Wednesday, March 2, 2016 7:49 PM
    Wednesday, March 2, 2016 3:05 PM

All replies

  • You can do the test directly in the if statement:

    If (Select-String -Path .\logFile2.txt -Pattern 'error phrase' -SimpleMatch) {
    
        Write-Host 'An error was found in the log' -ForegroundColor Red
    
    } Else {
    
        Write-Host 'No error detected' -ForegroundColor Green
    
    }
    
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> cat .\logFile1.txt
    garbage 1
    error phrase
    garbage 3
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> cat .\logFile2.txt
    garbage 1
    no error
    garbage 3
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> If (Select-String -Path .\logFile1.txt -Pattern 'error phrase' -SimpleMatch) {
    
        Write-Host 'An error was found in the log' -ForegroundColor Red
    
    } Else {
    
        Write-Host 'No error detected' -ForegroundColor Green
    
    }
    An error was found in the log
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> If (Select-String -Path .\logFile2.txt -Pattern 'error phrase' -SimpleMatch) {
    
        Write-Host 'An error was found in the log' -ForegroundColor Red
    
    } Else {
    
        Write-Host 'No error detected' -ForegroundColor Green
    
    }
    No error detected


    • Marked as answer by JoeFri Wednesday, March 2, 2016 7:49 PM
    Wednesday, March 2, 2016 3:05 PM
  • Get-Content C:\file.log | Where{ $_ -match 'error phrase'}


    \_(ツ)_/

    Wednesday, March 2, 2016 3:05 PM
  • I've tried -match, and I can't seem to match anything, b/c I think it has to match the entire line.  if I do "-like" with wildcards, it seems to work and return the entire line.

    what I need though is that my IF statement to be able to execute if that string exists, not necessarily the entire line.

    Wednesday, March 2, 2016 3:43 PM
  • Perhaps something like this:


    Get-Content "C:\file.log" | ForEach-Object { if ( $_ -like "*error string*" ) { $_ } }




    -- Bill Stewart [Bill_Stewart]

    Wednesday, March 2, 2016 4:05 PM
    Moderator
  • I don't think I need a for each, b/c I only need to know about a single line with that error string.  can you explain Bill?
    • Edited by JoeFri Wednesday, March 2, 2016 4:12 PM
    Wednesday, March 2, 2016 4:07 PM
  • That was just to illustrate reading the entire file.

    Have you tried Select-String?


    Select-String "error string" C:\file.log -SimpleMatch

    This will output any matches in the file. (-SimpleMatch means not to use a regular expression.)

    If you want to use this in an if test, use this:


    if ( (Select-String "error string" C:\file.log -SimpleMatch) ) {
      Write-Host "error string exists in file"
    }
    else {
      Write-Host "error string does not exist in file"
    }
    


    -- Bill Stewart [Bill_Stewart]



    • Edited by Bill_StewartModerator Wednesday, March 2, 2016 4:12 PM
    • Marked as answer by JoeFri Wednesday, March 2, 2016 7:48 PM
    • Unmarked as answer by JoeFri Wednesday, March 2, 2016 7:49 PM
    Wednesday, March 2, 2016 4:10 PM
    Moderator
  • I don't think I need a for each, b/c there I only need to know about a single line with that error string.  can you explain Bill?

    Have you tried what I posted? I thought I was pretty clear with my example.


    Wednesday, March 2, 2016 4:11 PM
  • Thanks Mike, does that simple match have to be exact and include everything on the line?  Or will it pull the string from the middle of the line?

    Typpically the error I'm trying to pull looks like this in the log:

    ***********************************

    ************ ERROR STRING *************

    ***************************************

    I only need the error string, and only want the If to execute if the string is there... don't necessarily need all the extra garbage.

    Wednesday, March 2, 2016 4:15 PM
  • Thanks Mike, does that simple match have to be exact and include everything on the line?  Or will it pull the string from the middle of the line?

    Typpically the error I'm trying to pull looks like this in the log:

    ***********************************

    ************ ERROR STRING *************

    ***************************************

    I only need the error string, and only want the If to execute if the string is there... don't necessarily need all the extra garbage.

    It'll work just fine with text in the middle:

    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> cat .\logFile3.txt
    garbage 1
    stuff and things error phrase things and stuff
    garbage 3
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-02-2016> If (Select-String -Path .\logFile3.txt -Pattern 'error phrase' -SimpleMatch) {
    
        Write-Host 'An error was found in the log' -ForegroundColor Red
    
    } Else {
    
        Write-Host 'No error detected' -ForegroundColor Green
    
    }
    An error was found in the log


    Wednesday, March 2, 2016 4:21 PM
  • You already have the error string, right?


    $errorString = "error string"
    
    if ( (Select-String $errorString C:\file.log -SimpleMatch) ) {
      Write-Host "$errorString exists in file"
    }
    else {
      Write-Host "$errorString does not exist in file"
    }
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, March 2, 2016 4:38 PM
    Moderator
  • I've tried -match, and I can't seem to match anything, b/c I think it has to match the entire line.  if I do "-like" with wildcards, it seems to work and return the entire line.

    what I need though is that my IF statement to be able to execute if that string exists, not necessarily the entire line.

    'match' does not work like "like"  "like" requires wildcards and "match" does not. THis will account for extra spaces:

    Get-Content C:\file.log | Where{ $_ -match 'error\s+phrase'}

    Try it like this:

    'This is my error  phrase that i want to match'  -match 'error\s+phrase'

    The where will find all lines that match.


    \_(ツ)_/

    Wednesday, March 2, 2016 5:56 PM
  • This worked like a charm, Thanks Mike!

    If (Select-String -Path .\logFile2.txt -Pattern 'error phrase' -SimpleMatch) {

    functions I wanted to run

    }

    My initial select-string command I was trying, I wasn't using the "-SimpleMatch" parameter. must have been why it didn't work the first time around.



    • Edited by JoeFri Wednesday, March 2, 2016 9:18 PM
    Wednesday, March 2, 2016 7:50 PM
  • Cheers, you're very welcome.

    Wednesday, March 2, 2016 8:32 PM