Thursday, July 14, 2011

Scripting and Configuration Manager Packages

Config Manager admins (like Altiris, etc.) are often splitting their time between deploying MSI, EXE application installers as well as custom scripts.  Quite often, and for good reason, they use CMD or BAT scripts to wrap up a sequence of things that would otherwise take longer or require more effort to do within a re-packager like InstallShield.  The problem that I see quite often is forgetting to return a meaningful result code to the Config Manager agent.

For example (BEFORE):

@echo off
msiexec /I “%~dp0myinstaller.msi” /quiet /norestart
cacls “%programfiles%\myApps” /e /t /c /g users:c
reg add HKLM\software\myApps\Fubar /v “Tarfu” /t REG_SZ /d “ABC” /f

If you don’t bother to explicitly return the %errorlevel% value you could be in for a very unhappy surprise.   A little dose of error checking helps a lot.  Here’s (just) one example:

(AFTER):

@echo off
set log=%temp%\myapp.log
echo %DATE% %TIME% msiexec /I “%~dp0myinstaller.msi” /quiet /norestart >>%LOG%
msiexec /I “%~dp0myinstaller.msi” /quiet /norestart
echo %DATE% %TIME% exit code %errorlevel% >>%LOG%
if %errorlevel%==0 (
   goto Configure
) else (
   if %errorlevel%==3010 (
      echo %DATE% %TIME% reboot required (suppressed) >>%LOG%
      goto Configure
   ) else (
      goto Failure
   )
)
goto END

:Continue
echo %DATE% %TIME% doing custom configuration changes… >>%LOG%
cacls “%programfiles%\myApps” /e /t /c /g users:c
reg add HKLM\software\myApps\Fubar /v “Tarfu” /t REG_SZ /d “ABC” /f
echo %DATE% %TIME% installation complete >>%LOG%
goto END

:Failure
echo %DATE% %TIME% installation failed: exit code %errorlevel% >>%LOG%
exit %errorlevel%

:END
exit %errorlevel%

Post a Comment