可以在PowerShell中使用条件管道吗?

Abe*_*ler 4 powershell

假设我只想将输出管道输出到另一个函数,具体取决于变量的值,是否有一些方法可以在powershell中执行此操作?

示例:

 if ($variable -eq "value"){$output | AnotherFunction}
  else {$output}
Run Code Online (Sandbox Code Playgroud)

@ mjolinor的例子是我现在正在做的事情,只是好奇是否有更好的方法来做到这一点.

mjo*_*nor 6

您可以使用If子句:

 if ($variable -eq "value"){$output | AnotherFunction}
  else {$output}
Run Code Online (Sandbox Code Playgroud)

作为fiter实现的示例:

 filter myfilter{
   if ($variable -eq "value"){$_ | AnotherFunction}
   else {$_}
 }
Run Code Online (Sandbox Code Playgroud)

然后:

 $variable = <some value>
 get-something | myfilter
Run Code Online (Sandbox Code Playgroud)