0

I am writing a script template to install multiple MSI's, including error logging and a rollback function. It contains scriptblocks to store install and uninstall information of each MSI.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:: Global Variables
SET PACKAGENAME=SysinternalsSuite
SET THISDIR=%~dp0
SET LOGFOLDER=%TEMP%\%PACKAGENAME%
IF EXIST "%LOGFOLDER%" RD "%LOGFOLDER%" /S /Q
MKDIR "%LOGFOLDER%"
SET LOGLEVEL=/lv

:: Scriptblock
SET /A INDEX=0
SET DISPLAYNAME[%I%]=SysinternalsSuite.msi
SET MSIPATH[%I%]="%thisdir%SysinternalsSuite.msi"
SET GUID[%I%]={79D7C0BA-9E50-44E8-8D0B-56D5EA692B5E}
SET ACTIONLOG[%I%]="%LOGFOLDER%\%I%_!DISPLAYNAME[%I%]!.log"
SET INSTALLCOMMAND[%I%]=MsiExec.exe /i !MSIPATH[%I%]! /qn %LOGLEVEL% !ACTIONLOG[%I%]!
SET UNINSTALLCOMMAND[%I%]=MsiExec.exe /x !GUID[%I%]! /qn
SET EXITATERROR[%I%]=1
SET ALLOWEDERROR[%I%]=3010

ECHO !INSTALLCOMMAND[%I%]!
ECHO !UNINSTALLCOMMAND[%I%]!

The idea is, that only the DISPLAYNAME, MSIPATH and GUID need to be defined, and the last 5 lines of the block will be part of a template. Is there a way I can use a marco for the last 5 lines of the scriptblock? I've tried with DOSKEY, but that doesn't seem to work in batch, and with all the percent signs and exclamation marks, it gets tricky. Any suggestions will be appreciated!

EDIT: Added some variables and ECHO commands, to illustrate what the output should be; valid MsiExec commandlines.

jnoort
  • 71
  • 4
  • If the last five variables are the same, why create them with an index? There is a limit on environment variable space. – lit May 01 '16 at 16:06
  • The value will not be the same, but the install command and uninstall commands will have the same definition in terms of other variables. In other words: The lines will be the same, but the values should end up being different for each MSI block. – jnoort May 09 '16 at 21:01

1 Answers1

1

I feel like such a n00b, but I basically found out how to eliminate the five lines of code, by the scriptblock to:

:: Scriptblock
SET /A INDEX=0
SET DISPLAYNAME[%I%]=SysinternalsSuite.msi
SET MSIPATH[%I%]="%thisdir%SysinternalsSuite.msi"
SET GUID[%I%]={79D7C0BA-9E50-44E8-8D0B-56D5EA692B5E}
Call :BuildCommandLines

And adding a function:

:BuildCommandLines
    SET ACTIONLOG[%I%]="%LOGFOLDER%\%I%_!DISPLAYNAME[%I%]!.log"
    SET INSTALLCOMMAND[%I%]=MsiExec.exe /i !MSIPATH[%I%]! /qn %LOGLEVEL% !ACTIONLOG[%I%]!
    SET UNINSTALLCOMMAND[%I%]=MsiExec.exe /x !GUID[%I%]! /qn
    SET EXITATERROR[%I%]=1
    SET ALLOWEDERROR[%I%]=3010
goto :eof
jnoort
  • 71
  • 4