简体   繁体   中英

Writing a batch script to start services

I'm writing a script that will start Windows services that are supplied within a defined list:

set SERVICE_LIST=( "service1" "service2" )

for %%A in %SERVICE_LIST% do (
    net start %%A
    if %ERRORLEVEL% EQU 0 echo "...Service [%%A] started successfully" & goto :nextservice
    if %ERRORLEVEL% NEQ 0 echo "...There was an issue starting service [%%A]"
    :nextservice
)

When I run the script I get the following error:

) was unexpected at this time.

Am I unable to define all of the error catching for each service within the do() block? Is there a better way to achieve what I am trying to do?

set "SERVICE_LIST=service1,service2,..."

FOR %%A in (%SERVICE_LIST%) DO ...

The error is caused by the :nextservice line: you should not include labels nor goto commands inside a ( block ) . BTW it is a bad practice to modify the syntax of commands storing parts of them in a variable...

set SERVICE_LIST="service1" "service2"

for %%A in (%SERVICE_LIST%) do (
    net start %%A
    if ERRORLEVEL 1 (
       echo "...There was an issue starting service [%%A]"
    ) else (
       echo "...Service [%%A] started successfully"
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM