0

I am executing a batch script to perform operations on services in a server. so i am giving a ui to user to select a list of services from a server. i cab perform this operation if user selects only one service. I want to know how to perform if users selects multiple services as this services will be passed as a parameter to the batch file. I tried this for a single service.

set /a ArgCount = 0
for %%a in (%*) do (
set /a ArgCount += 1
set "argVec[!ArgCount!]=%%~a"
)
if !ArgCount! LSS 2 ( 
echo Arguments missing 
goto :eof
)
    set server=%1
    set serviceName=%2`


    SC \\%server% query  "%serviceName%" | find "STATE" | find "RUNNING"
    If ERRORLEVEL 1 goto :start

    :start
    cmdkey /add:%server% /user:%username% /pass:%password%
    sc \\%server% start "%ServiceName%"

I want to know how to modify this script when user selects more than one or multiple services on remote desktop.

Anonymous
  • 5
  • 2

1 Answers1

0

This batch doesn't use the argcount but simply stores %1 to servername and then uses shift in a loop to process the services until all done.

@Echo off
set server=%1
:loop
shift
If "%~1" Equ "" goto :eof
set serviceName=%1

SC \\%server% query  "%serviceName%" | findstr "STATE.*RUNNING" && Goto :loop

cmdkey /add:%server% /user:%username% /pass:%password%
sc \\%server% start "%ServiceName%"
Goto :loop
LotPings
  • 1,015
  • 7
  • 12
  • serviceName will be %2...and can u please explain this – Anonymous Jun 28 '17 at 06:27
  • first the var serverName is populated from `%1` then the shift command shifts args one down the line `%2` to `%1`, `%3` to `%2` etc. The check inside the looped area if %1 is empty ends the batch when all arguments are processed. – LotPings Jun 28 '17 at 12:43