powershell - 在 .BAT 程序中获取 Powershell 返回代码
问题描述
我有一个 .BAT 程序,我在其中调用 Powershell 脚本来运行。
powershell 完成工作后,如何在 .BAT 程序中获取退出代码?
编辑:
这对我有用。
call powershell.exe .\MyPowerShellScript.ps1
echo %ERRORLEVEL%
解决方案
该%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
使用脚本,或者使用-File
参数:
@echo off
call powershell.exe -File "path\to\file.ps1"
echo Exited with return code %ERRORLEVEL%
或使用&
调用运算符调用脚本:
@echo off
call powershell.exe -Command "& 'C:\path\to\file.ps1'"
echo Exited with return code %ERRORLEVEL%
推荐阅读
- android - Android Studio - 按钮未重定向
- python - 如何在 CSS 选择器中创建可迭代循环
- html - 如何防止浮动嵌套 div 向下移动内容
- php - 如何在 laravel phpunit.xml 中添加 elasticsearch 测试数据库作为第二个测试数据库?
- sql - 如何进行此选择连接查询
- python - 使用 openpyxl 读取 xlsx:公式计算结果为空/空白
- time-complexity - 中位数的时间复杂度
- python - 如何通过读取将具有 str 值的 CSV 数据格式化为浮点值?
- grails - grails无法从插件中找到视图
- compiler-errors - 使用 CMake 编译链接器错误 MacOS c++