编写批处理脚本以启动服务

时间:2017-09-11 14:23:06

标签: windows batch-file service

我正在编写一个脚本,它将启动定义列表中提供的Windows服务:

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
)

当我运行脚本时,我收到以下错误:

) was unexpected at this time.

我无法定义do()块中每个服务的所有错误捕获?有没有更好的方法来实现我想要做的事情?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

错误是由:nextservice行引起的:您不应在goto中包含标签( block )命令。顺便说一句,在变量中修改存储部分命令的语法是一种不好的做法......

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"
    )
)