我有一个.BAT程序,在其中调用Powershell脚本来运行。
Powershell完成工作后,如何在.BAT程序中获得退出代码?
%ERRORLEVEL%
变量将为您提供最后的返回代码:
@echo off
call powershell.exe -Command "exit 123"
echo Exited with return code %ERRORLEVEL%
将导致:
C:\> path\to\script.bat
Exited with return code 123
脚本终止异常也会设置错误级别。
powershell throw
ScriptHalted
At line:1 char:1
+ throw
+ ~~~~~
+ CategoryInfo : OperationStopped: (:) [], RuntimeException
+ FullyQualifiedErrorId : ScriptHalted
echo %errorlevel%
1