Windows – Batch script with multiple parameters

batchremote-desktop-serviceswindows

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.

Best Answer

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