How to capture error output only in a variable in PowerShell

后端 未结 2 1971
暖寄归人
暖寄归人 2021-02-18 15:57

I want to store the stderr output of a PowerShell command in a variable. I don\'t want to store it in a file and I don\'t want standard output included, just the error output.

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-18 16:11

    You can call the command a slightly different way and use the -ErrorVariable parameter in PowerShell:

    Invoke-Expression "$command $params" -ErrorVariable badoutput
    

    $badoutput will now contain the contents of the error string.

提交回复
热议问题