Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell if statement in pipeline

I am in need to create a script which can continuously read a file and if a certain line with text comes, to call a function.

I am thinking in a way to do this a single line thing with

Get-Content -Path $logFile -wait | ?{$_-match "$filter"}

but I am not aware of a way to call a function if the line does answer the criteria.

Is that doable ? or I should do it like in a loop to read the file every cycle and if there is a match to exit the loop ?

like image 593
Martin Nikolaev Avatar asked Dec 20 '22 17:12

Martin Nikolaev


1 Answers

Pipe it to a foreach loop, which will go through each line individually, and you can put the if statement in the loop:

Get-Content -Path $logFile -wait | %{if($_ -match "$filter"){Callfunction}}
like image 150
Cole9350 Avatar answered Feb 24 '23 05:02

Cole9350