Is there a way to specify a font color when using write-output

前端 未结 5 1591
抹茶落季
抹茶落季 2021-02-07 02:25

I have a powershell script that gives some status output via write-output. I am intentionally not using write-host because the output may be captured and writt

5条回答
  •  囚心锁ツ
    2021-02-07 02:55

    Separate the results on the pipeline from the status messages in the console.

    E.g., use a function like this in your script:

    function write-status( $status ){
       $status | write-host -fore green -back red;  #send a status msg to the console
       $status | write-output; #send a status object down the pipe
    }
    

    I would also recommend you use one of the following cmdlets over write-host for outputting status messages from your scripts:

    • write-debug
    • write-error
    • write-verbose
    • write-warning

    The appearance of these status messages will vary depending on the cmdlet used. In addition, the user can disable specific levels of status using the $(warning|error|verbose|debug)preference variables, or capture specific status messages using the -(warning|error|verbose|debug)variable common cmdlet parameters.

提交回复
热议问题