Wednesday, October 24, 2012

Creating a batch file that accepts parameters

We are App-V'ing a bunch of our applications and one of the applications requires a different .ini file depending if it's test, production or training.  To get around creating 3 different packages with just a single file, I've written a pre-launch batch file that will auto-create the .ini depending on the parameters passed to it.

:===========================================================================================
:         Paris Pre-launch script for App-V
:         By Trentent Tye
:         2012-10-24
:
:
:         This command file will take numerous parameters for launching the specific type
:         of Paris application.  It will autogenerate an INI file that Paris uses on
:         launch to self configure.  If no parameter is passed, it's assumed to be
:         commented out in the INI file.
:         This file should be launched like so:
:         paris.cmd PARA request=0 hostname=nice servergroup=development_3861
:
:         it will automatically find the variable without an equal sign and assume
:         that it is the alias.  Everything else gets matched up to the [Host]
:         section of the .ini




:1) Pull string off the command line
:2) filter out " and '
:3) break each set out by spaces
:4) save to temp file
:5) iterate through temp file (for) and create variables.

@ECHO OFF

ECHO Launching Paris
SET PARAMS=%*

:NOTE: We are replacing quotations with whitespace characters!  Ensure there is no
:whitespace in the parameters, but whitespace *between* parameters.

call set PARAMS=%%PARAMS:"= %%
call set PARAMS=%%PARAMS:'= %%

ECHO %PARAMS%

:Break each set of parameters into it's own set seperated by the equals sign.
:If there is no parameter stored we "breakout"
for /f "tokens=1-26 delims= " %%A IN ("%PARAMS%") DO (
IF '%%A' EQU '' GOTO BREAKOUT
ECHO %%A > "%TEMP%\variables.txt"
IF '%%B' EQU '' GOTO BREAKOUT
ECHO %%B >> "%TEMP%\variables.txt"
IF '%%C' EQU '' GOTO BREAKOUT
ECHO %%C >> "%TEMP%\variables.txt"
IF '%%D' EQU '' GOTO BREAKOUT
ECHO %%D >> "%TEMP%\variables.txt"
IF '%%E' EQU '' GOTO BREAKOUT
ECHO %%E >> "%TEMP%\variables.txt"
IF '%%F' EQU '' GOTO BREAKOUT
ECHO %%F >> "%TEMP%\variables.txt"
IF '%%G' EQU '' GOTO BREAKOUT
ECHO %%G >> "%TEMP%\variables.txt"
IF '%%H' EQU '' GOTO BREAKOUT
ECHO %%H >> "%TEMP%\variables.txt"
IF '%%I' EQU '' GOTO BREAKOUT
ECHO %%I >> "%TEMP%\variables.txt"
IF '%%J' EQU '' GOTO BREAKOUT
ECHO %%J >> "%TEMP%\variables.txt"
IF '%%K' EQU '' GOTO BREAKOUT
ECHO %%K >> "%TEMP%\variables.txt"
IF '%%L' EQU '' GOTO BREAKOUT
ECHO %%L >> "%TEMP%\variables.txt"
)
:BREAKOUT


:Now we iterate through the temp file and auto-create the Paris.ini
for /f "tokens=1-2 delims==" %%A IN ('TYPE "%TEMP%\variables.txt"') DO (
IF /I '%%A' EQU 'REQUEST' set REQUEST=%%B
IF /I '%%A' EQU 'OBJECTNAME' set OBJECTNAME=%%B
IF /I '%%A' EQU 'HOSTNAME' set HOSTNAME=%%B
IF /I '%%A' EQU 'REQUESTBROKER' set REQUESTBROKER=%%B
IF /I '%%A' EQU 'RequestBrokerBackup' set RequestBrokerBackup=%%B
IF /I '%%A' EQU 'ServerGroup' set ServerGroup=%%B
IF /I '%%B' EQU '' set ALIAS=%%A
)

:Now that we have all the parameters for the ini file we can create it.

SET PARISPATH="%TEMP%\paris.ini"

ECHO [PreLogin] >"%PARISPATH%"
ECHO ClientDLL=STAPPClientLogin >>"%PARISPATH%"
ECHO FunctionName=DoLogin >>"%PARISPATH%"
ECHO ServerDLL=STServerLogin >>"%PARISPATH%"
ECHO DataModule=Login >>"%PARISPATH%"
ECHO. >>"%PARISPATH%"
ECHO [PostLogin] >>"%PARISPATH%"
ECHO ServerDLL=STSRVLogin >>"%PARISPATH%"
ECHO DataModule=Login >>"%PARISPATH%"
ECHO. >>"%PARISPATH%"
ECHO [Timer] >>"%PARISPATH%"
ECHO Enabled=0 >>"%PARISPATH%"
ECHO Interval=10000 >>"%PARISPATH%"
ECHO. >>"%PARISPATH%"
ECHO [Host] >>"%PARISPATH%"


if /I "%REQUEST%" EQU "" ECHO #Request=0 >>"%PARISPATH%"
if /I "%REQUEST%" NEQ "" ECHO Request=%REQUEST% >>"%PARISPATH%"

if /I "%OBJECTNAME%" EQU "" ECHO #objectname=I4AppServer >>"%PARISPATH%"
if /I "%OBJECTNAME%" NEQ "" ECHO objectname=%OBJECTNAME% >>"%PARISPATH%"

if /I "%HOSTNAME%" EQU "" ECHO #hostname=AComputerName >>"%PARISPATH%"
if /I "%HOSTNAME%" NEQ "" ECHO hostname=%HOSTNAME% >>"%PARISPATH%"

if /I "%REQUESTBROKER%" EQU "" ECHO #RequestBroker=AComputerName >>"%PARISPATH%"
if /I "%REQUESTBROKER%" NEQ "" ECHO RequestBroker=%REQUESTBROKER% >>"%PARISPATH%"

if /I "%RequestBrokerBackup%" EQU "" ECHO #RequestBrokerBackup=AnotherComputerName >>"%PARISPATH%"
if /I "%RequestBrokerBackup%" NEQ "" ECHO RequestBrokerBackup=%RequestBrokerBackup% >>"%PARISPATH%"

if /I "%ServerGroup%" EQU "" ECHO #ServerGroup=AnotherComputerName >>"%PARISPATH%"
if /I "%ServerGroup%" NEQ "" ECHO ServerGroup=%ServerGroup% >>"%PARISPATH%"


ECHO. >>"%PARISPATH%"
ECHO [Settings] >>"%PARISPATH%"
ECHO MaxLookup=200 >>"%PARISPATH%"
ECHO NotifyInterval=30000 >>"%PARISPATH%"
ECHO #Interval between refresh of InBox screen (1second=1000) >>"%PARISPATH%"
ECHO Helpfile=paris1.chm >>"%PARISPATH%"


:ADD COPY COMMAND
:ADD LAUNCH COMMAND
:copy /y "%PARISPATH%" "B:\In4tek_Paris_37_MNT\Paris\bin\STAPPParisShellDCOM.INI"
ECHO "B:\In4tek_Paris_37_MNT\Paris\bin\STAPPParisShellDCOM.exe -alias %ALIAS%
type "%PARISPATH%"


No comments: