How do I write to standard error in PowerShell?

前端 未结 3 546
遥遥无期
遥遥无期 2020-11-30 04:00

I\'m having trouble figuring out how to both echo to the standard error stream and redirect the error stream of an executable.

I have come from a Bourne shell and Ko

3条回答
  •  渐次进展
    2020-11-30 04:17

    Use Write-Error to write to stderr. To redirect stderr to file use:

     Write-Error "oops" 2> /temp/err.msg
    

    or

     exe_that_writes_to_stderr.exe bogus_arg 2> /temp/err.msg
    

    Note that PowerShell writes errors as error records. If you want to avoid the verbose output of the error records, you could write out the error info yourself like so:

    PS> Write-Error "oops" -ev ev 2>$null
    PS> $ev[0].exception
    oops
    

    -EV is short (an alias) for -ErrorVariable. Any errors will be stored in the variable named by the argument to this parameter. PowerShell will still report the error to the console unless we redirect the error to $null.

提交回复
热议问题