Martin Nikolaev
Martin Nikolaev

Reputation: 233

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 ?

Upvotes: 4

Views: 22509

Answers (1)

Cole9350
Cole9350

Reputation: 5570

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}}

Upvotes: 12

Related Questions