Click here to Skip to main content
15,947,958 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 . . . .
Posted
Comments
Hamdi Choban 16-May-14 7:56am    
what you mean by "exit code" , if it is in string format then you can write saving code for it inside your MybatchFile.bat for example : exit_code > c:\exit_code_content.txt .

1 solution

Hi,
After doing a lot of research on google ,I myself found the solution.

I used new PowerShell script to execute batch file in PowerShell and to get exitCodes of batch files to PowerShell .My new PowerShell script given below
param( 
	[String]$FilePath="D:\Scripts\MyBatchFile.bat 'C:\PSInstall' 'D:\TempDelete' 'C:\PSInstall\cert' AdminId AdminPwd 'Prod'" )

function ABC()
{

try{
$Command="$FilePath"


cmd /c $Command |Out-String



exit 0

 }
 catch{
 
 write-host "-----------------------------------------------------------" -ForegroundColor Green
 write-host "Caught an exception:" -ForegroundColor Red
  write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
  write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
 
 
  write-host "-----------------------------------------------------------" -ForegroundColor Green
  exit 1
 }
 
write-host "The Last Exit Code is:" $LastExitCode
 }
 $ErrorActionPreference = "Stop"
 
 ABC


above script totally solved my problem.

$LastExitCode returns 0 if success and 1 if failure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900