-command's exit code is not the same as a script's exit code

后端 未结 3 1922
深忆病人
深忆病人 2020-12-06 04:05

I need to run a script with PowerShell -Command \"& scriptname\", and I would really like it if the exit code I got back from PowerShell was the same as the exit code th

3条回答
  •  自闭症患者
    2020-12-06 04:51

    CAVEAT: If your PowerShell script returns exitcodes HIGHER THAN 65535, they roll over:

    $exitCode = 65536
    Exit $exitCode
    

    If the following CMD calls this PS1 script above, your will get an %errorlevel% of 0

    Powershell.exe "& 'MyPowershellScript.ps1' "; exit $LASTEXITCODE
    SET ERR=%ERRORLEVEL%
    

    and an exitcode of 65537 would give you an %errorlevel% of 1, etc.

    Meanwhile, if a CMD calls another and the child script returns an errorlevel higher than 65535, it passes through just fine.

    Cmd /c exit 86666
    

    The CMD Will return an %errorlevel% of 86666 as expected.

    CAVEAT to all of this: Now this is happening on and off for no apparent reason.

提交回复
热议问题