在.BAT程序中获取Powershell返回代码

问题描述 投票:0回答:2

我有一个.BAT程序,在其中调用Powershell脚本来运行。

Powershell完成工作后,如何在.BAT程序中获得退出代码?

powershell batch-file batch-processing
2个回答
1
投票

%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

0
投票

脚本终止异常也会设置错误级别。

powershell throw

ScriptHalted
At line:1 char:1
+ throw
+ ~~~~~
    + CategoryInfo          : OperationStopped: (:) [], RuntimeException
    + FullyQualifiedErrorId : ScriptHalted


echo %errorlevel%

1
© www.soinside.com 2019 - 2024. All rights reserved.