Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to have conditional pipes in PowerShell?

Tags:

powershell

Say I only want to pipe my output to another function depending on the value of a variable, is there some way to do this in powershell?

Example :

 if ($variable -eq "value"){$output | AnotherFunction}
  else {$output}

@mjolinor's example is how I am doing it now, just curious if there is a better way to do it.

like image 315
Abe Miessler Avatar asked May 19 '11 16:05

Abe Miessler


2 Answers

You can use an If clause:

 if ($variable -eq "value"){$output | AnotherFunction}
  else {$output}

Example implemented as a fiter:

 filter myfilter{
   if ($variable -eq "value"){$_ | AnotherFunction}
   else {$_}
 }

Then:

 $variable = <some value>
 get-something | myfilter
like image 196
mjolinor Avatar answered Oct 15 '22 21:10

mjolinor


You may get the results you are looking for by piping to the Where-Object.

Example:

My-Command | Where-Object {$variable -eq "value"} | AnotherFunction

like image 29
daalbert Avatar answered Oct 15 '22 20:10

daalbert