How to get status of “Invoke-Expression”, successful or failed?

前端 未结 5 1209
故里飘歌
故里飘歌 2021-01-01 17:19

Invoke-Expression will return all the text of the command being invoked.

But how can I get the system return value of whether this command has been exec

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 17:49

    I found a simple way to do it, which keeps STDOUT in tact.

    $Expr="MY EXPRESSION"
    $Expr += '; $Success=$?'
    Invoke-Expression $Expr
    

    Success is now True or False, and all IO remains intact.

提交回复
热议问题