How to programmatically determine success or failure when installing MSI?

I have a bootstrapper application that installs several MSI packages. However, it seems that the Windows installer does not return an error code if the installation failed. For example, the following command line test does not print “crash” if I hit “cancel”:

msiexec /i myinstaller.msi || echo failed 

Given the lack of error feedback, what is the best way to detect installation failure?

<h / "> As the accepted answer suggests, the error code is actually returned. For some reason, my test case only works as expected when it is run from a batch file rather than being entered directly on the command line.

+4
windows-installer
Nov 10 '09 at 14:46
source share
4 answers

In fact, msiexec returns error codes, two success codes are 0 (success) and 3010 (success, a reboot is required). Maybe cmd.exe does some unwanted magic in your example (for example, returning before msiexec completed), but I successfully read the msiexec error codes when doing this through VBScript WScript. Shell Run (with bWaitOnReturn = True).

Try throwing the following in test.vbs and then running it with cscript test.vbs :

 Set WshShell = WScript.CreateObject("WScript.Shell") MsgBox(WshShell.Run("msiexec /i myinstaller.msi", , true)) 

It should appear with a non-zero value if you click Cancel.

+4
Nov 10 '09 at 2:55 a.m.
source share

If you click on cancel, this is not an error, the installation program will perform the requested action and most likely will return 0 to the cancel function.

+3
Mar 25 2018-11-11T00:
source share

Since Windows Installer 1.0 was first released, msiexec.exe always starts in the Windows subsystem. This means that when it is executed from the console or a batch script, the control returns to the console or script immediately. If you depend on the %ERRORLEVEL% variable, which is respectively, it will not.

In this scenario, I like to use start /wait from the command line or a script package. This will create the process and wait for it to exit, and the return code from the process is passed and returned from the start command, so %ERRORLEVEL% set accordingly. Just type start /wait before the command line, you usually go to msiexec.exe, as in the following example:

start /wait msiexec.exe /i netfx.msi /l*v netfx.log

The script package will be blocked, and then until msiexec.exe shuts down. Programmatically, this is no different from calling msiexec.exe with CreateProcess and waiting for the process handle to be signaled by WaitForSingleObject without a timeout.

Source: https://blogs.msdn.microsoft.com/heaths/2005/11/15/waiting-for-msiexec-exe-to-finish/

Code example:

 start /wait msiexec.exe /i netfx.msi /l*v netfx.log if "%errorlevel%" == "0" goto OK if "%errorlevel%" == "1013" goto err if "%errorlevel%" == "1603" goto err if not "%errorlevel%" == "0" goto err :OK GOTO END :err rem print message and return errorlevel so package errors echo "Error: Msiexec failed with errorlevel = %errorlevel%" exit /b %errorlevel% :END 

Link to the code: https://www.computing.net/answers/windows-xp/batch-file-to-install-msi-and-check-errorlvl/178657.html

+2
Feb 20 '17 at 23:51 on
source share

msiexec does returns an error if the installation fails. To catch the user, you may need a MIF file.

+1
Nov 10 '09 at 14:56
source share



All Articles