1

I tried to use the suggestion at the following post;

How to stop Get-Content -wait after I find a particular line in the text file using powershell?

...but the Get-Content command seems to continue when I run the command as follows (server name edited). I.e. when the string "FATAL" is reached, the Get-Content continues.

Get-Content \\n------v\c$\ProgramData\Navis\center\logs\navis-apex.log -wait -Tail 1 | % {$_ ; if($_ -eq "FATAL") {break}}

enter image description here

Trevor H
  • 35
  • 3
  • The only specific problem with your code is likely that you're matching the word `FATAL` against the _whole line_ (`-eq`) instead of looking for it _as a substring_ on the line, as shown in Jawad's answer. However, the code you quote from the linked post has an important flaw: `break` is not designed to exit a _pipeline_: in the absence of an enclosing _loop_ it _exits the script as a whole_ - see Jawad's answer and also [this one](https://stackoverflow.com/a/60341948/45375) for more background information. – mklement0 Feb 21 '20 at 16:12
  • Thanks all for the responses, which have seemed to resolve the issue. :) – Trevor H Feb 23 '20 at 22:33

2 Answers2

2

If you are reading in lines, you cant check the whole line against a word.

Get-Content \\n----v\c$\ProgramData\Navis\center\logs\navis-apex.log -wait -Tail 1 |
  % {$_ ; if($_ -match "FATAL") {break}}

You want to check the content and see if it contains the word, use the -match or -like operators.

Caveats and workarounds

I want to add that, if you have code after this, it will not be executed. as @mklement0 pointed out, short of using break with a dummy loop around the pipeline, there is currently no way to exit a pipeline prematurely

Get-Content C:\temp\file.txt -wait -Tail 1 | % { if ($_ -match "EXIT") {"found the match"; break;} }
Write-Output "Printing Next Statement" # Will not execute.. script exited already.

#outputs
found the match

workaround 1: try/catch with a throw statement.

try {
    Get-Content C:\temp\file.txt -wait -Tail 1 | % { if ($_ -match "EXIT") {"found the match"; throw "Exiting loop";} }
}
catch {
    Write-Output "All Contents Retreived."
}

Write-Output "Printing Next Statement"

#Outputs
found the match
All Contents Retreived.
Printing Next Statement

workaround 2 Use of a dummy loop.

while ($true) {
    Get-Content C:\temp\file.txt -wait -Tail 1 | % { if ($_ -match "EXIT") {"found the match"; break;} }
}
Write-Output "Printing Next Statement"

#outputs
found the match
Printing Next Statement
mklement0
  • 382,024
  • 64
  • 607
  • 775
Jawad
  • 11,028
  • 3
  • 24
  • 37
1

This works nicely for me :

Get-Content "\\path_to\log.log" -wait -Tail 1 | % {   
        if($_ -match "FATAL")
        {   write-warning $_
            break
        }
        else
        {   write-host $_
        }
    }
Civette
  • 386
  • 2
  • 10
  • `break` is not designed to exit a _pipeline_: in the absence of an enclosing _loop_ it _exits the script as a whole_; there is currently _no_ direct way to exit a pipeline prematurely, though adding such a feature is the subject of a [long-standing proposal](https://github.com/PowerShell/PowerShell/issues/3821). A - limited - workaround is to use a _dummy loop_ around the pipeline; see [this answer](https://stackoverflow.com/a/60341948/45375). – mklement0 Feb 21 '20 at 15:57