Is there a way to set a variable up to place output to stdout or null?

前端 未结 3 1869
北恋
北恋 2020-12-07 00:05

I would like to set up a variable in my code that would ultimately define if I\'ll see some output or not.

  • \"hello\" writes to stdout
3条回答
  •  情深已故
    2020-12-07 00:19

    All right. Thanks to all the brainiacs here for the motivation. This answer may not be the best way to go about it, but it works!

    Two things you need to understand to achieve this:

    1. If you are used to using Write-Host, it won't work, you'll have to go with Write-Output.
    2. You may have to learn to use a block of script as a function parameter.

    One is self explanatory, so here's how to attain #2:

    Function Test-SctiptBlockParam {
      Param(
        $scriptblock
      )
      if ($debugOutput) {
        Invoke-Command $scriptblock
      } else {
        (Invoke-Command $scriptblock) > $null
      }
    }
    
    Test-SctiptBlockParam -scriptblock { Write-Output "I want to see on the STDOUT sometimes" }
    

    Finally, here is an example of my output and code

提交回复
热议问题