如何将批处理文件退出代码返回到PowerShell脚本? [英] How to return batch file exit code to powershell script ?
本文介绍了如何将批处理文件退出代码返回到PowerShell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi,
I am executing a batch file from PowerShell .I have PowerShell script as shown below which runs .bat file.
$process = [WMICLASS]"\\server\ROOT\CIMV2:win32_process"
$result = $process.Create("D:\Scripts\MyBatchFile.bat 'C:\PSInstall' 'D:\TempDelete' 'C:\PSInstall\cert' AdminId AdminPwd 'Prod'")
Here my batch file is MyBatchFile.bat and I am passing some parameters to it.
In batch file I used below commands to deal with errors. As shown below my batch file returns exit code 0 if success and returns 1 if any error.
IF %ERRORLEVEL% neq 0 goto errorsection
@echo Executed MyBatchFile successfuly
echo -------------------------------------------------------------------
echo Finished Processing MyBatchFile.bat
echo -------------------------------------------------------------------
echo.
echo **************************************************************************************
exit 0
:errorsection
@echo Error found in executing MyBatchFile
echo -------------------------------------------------------------------
echo Finished Processing MyBatchFile.bat
echo -------------------------------------------------------------------
echo.
echo **************************************************************************************
exit 1
Now I want to store that exit code returned by MyBatchFile.bat file in a PowerShell variable.
How to get batch file exit code to PowerShell variable ?
I have tried $LastExitCode in PowerShell to capture the exit code returned by MyBatchFile.bat file, but didn't worked. I have provided PowerShell script I used to run batch file . How can I capture batch file exit code using above PowerShell script ?
Please suggest me . . . .
推荐答案
process = [WMICLASS]\\server\ROOT\CIMV2:win32_process
process = [WMICLASS]"\\server\ROOT\CIMV2:win32_process"
< br>
result =
process.Create(D:\Scripts\MyBatchFile.bat'C:\ PSInstall''D:\ TempDelete''C :\ PSInstall\cert'AdminId AdminPwd'Prod')
这里我的批处理文件是MyBatchFile.bat,我将一些参数传递给它。
在批处理文件中,我使用下面的命令来处理错误。如下所示,我的批处理文件如果成功则返回退出代码0,如果有任何错误则返回1。
IF%ERRORLEVEL%neq 0 goto errorsection
@echo成功执行MyBatchFile
echo -------------------- -----------------------------------------------
echo完成处理MyBatchFile.bat
echo -------------------------------------- -----------------------------
echo。
echo ********************************************* *****************************************
退出0
:errorsection
@echo在执行MyBatchFile
echo时发现错误--------------------------- ----------------------------------------
echo Finished Processing MyBatchFile.bat
echo --------------------------------------------- ----------------------
echo。
echo ********************************************* *****************************************
退出1
现在我想将MyBatchFile.bat文件返回的退出代码存储在PowerShell变量中。
如何将批处理文件退出代码转换为PowerShell变量?
我试过
process.Create("D:\Scripts\MyBatchFile.bat 'C:\PSInstall' 'D:\TempDelete' 'C:\PSInstall\cert' AdminId AdminPwd 'Prod'") Here my batch file is MyBatchFile.bat and I am passing some parameters to it. In batch file I used below commands to deal with errors. As shown below my batch file returns exit code 0 if success and returns 1 if any error. IF %ERRORLEVEL% neq 0 goto errorsection @echo Executed MyBatchFile successfuly echo ------------------------------------------------------------------- echo Finished Processing MyBatchFile.bat echo ------------------------------------------------------------------- echo. echo ************************************************************************************** exit 0 :errorsection @echo Error found in executing MyBatchFile echo ------------------------------------------------------------------- echo Finished Processing MyBatchFile.bat echo ------------------------------------------------------------------- echo. echo ************************************************************************************** exit 1 Now I want to store that exit code returned by MyBatchFile.bat file in a PowerShell variable. How to get batch file exit code to PowerShell variable ? I have tried
这篇关于如何将批处理文件退出代码返回到PowerShell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文