Checking exit code of last command using “if” statement

前端 未结 2 1776
慢半拍i
慢半拍i 2020-12-09 09:32

I want to check the status of last command and based on the exit code, the commands will be executed further.

The last command execute was:

$hiveJob         


        
2条回答
  •  醉梦人生
    2020-12-09 09:42

    You're talking about "exit code". If you mean $LastExitCode automatic variable, it is only populated when you call windows program, RAR for example:

    $x=rar
    $LastExitCode
    

    It will return exit code 7 (if you have RAR installed).

    cmdlets, however, don't fill this variable. You can use another automatic variable $? for this:

    $x=gci
    $?
    

    It only gives $True if command completed successfully or $False if there was an error.

提交回复
热议问题