Reporting on ERRORLEVEL in Batch Scripts?
I am working on creating a standard wrapper of sorts around my post-installation scripts, with an introduction, execution of installation package, error-catching, and notification of script end.
My method of detecting any error has been as such:
@ECHO OFF
echo ****************** echo Installing Aleks... msiexec /i %~dp0aleks318.msi /qb
IF %ERRORLEVEL% EQU 0 GOTO success GOTO error
:success echo Aleks Installed. echo ****************** GOTO END
:error echo An error %ERRORLEVEL% occured. >> %systemroot%\Aleks_Error.log GOTO END
:end
So I am wondering if I should expect software installation packages to not always leave ERRORLEVEL as 0, even when the software installed without error.
Has anybody else set up a similar sort of mechanism? Is there any better ways to check for errors?
Answers (3)
Try using "START /WAIT" in front of your line for the install using "MSIEXEC" and see if it provides the correct results. This should make the script wait for the installation to finish and read the correct value of the ErrorLevel. Also, be sure that the path to the MSI does not contains spaces or special characters that would require the path to be enclosed in quotes. If you pass an invalid path (or GUID) to MSIEXEC it will not update the ErrorLevel.
@ECHO OFF
echo ******************
echo Installing Aleks...
START /WAIT msiexec /i %~dp0aleks318.msi /qb
IF %ERRORLEVEL% EQU 0 GOTO success
GOTO error
:success
echo Aleks Installed.
echo ******************
GOTO END
:error
echo An error %ERRORLEVEL% occured. >> %systemroot%\Aleks_Error.log
GOTO END
:end