How to capture error messages thrown by a command?

后端 未结 3 616
南笙
南笙 2020-12-29 12:47

I am writing a PowerShell script where in I need to capture the error message that it\'s throwing. Note: according to PowerShell, there is no error and command is executed s

3条回答
  •  囚心锁ツ
    2020-12-29 13:24

    Inspired by David Brabants answer, you can combine both stdout and stderr into one string array using this command:

    $output = [string[]] (.\Cli.exe -p $param 2>&1)
    

    It will execute Cli.exe with the parameter p. This is optionally.

    Clarification

    2>&1 means the stream #2 (stderr) will be redirected to stream #1 (stdout), thus both streams will arrive in $output.

提交回复
热议问题