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