使用SC命令在一个脚本中启动多个服务

时间:2013-10-16 23:09:41

标签: batch-file

我有三个服务(“SampleService”,“SampleService1”和“SampleService2”),我想开始使用单个脚本,如果有人能告诉我如何在下面的代码中完成此任务,我将不胜感激。

@ECHO OFF


REM  ---------------------------------
REM | *** Define script variables *** |
REM  ---------------------------------

    enter code here

SET serviceDisplayName="SampleService1"
SET logFile=c:\logs\StartService.log
SET /A waitSeconds=10


REM  ---------------------------------------------------------------------------------------
REM | *** GET THE KEYNAME OF THE SERVICE (NEEDED FOR "SC START" AND "SC STOP" COMMANDS) *** |
REM  ---------------------------------------------------------------------------------------

ECHO. >> %logFile%
ECHO ####### [%date%] Stop Service %serviceDisplayName% Log #######>> %logFile%

FOR /F "tokens=1-4 skip=1 delims= " %%a IN ('SC GetKeyName %serviceDisplayName%') DO (
    SET serviceKeyName=%%c
)

IF /i "%serviceKeyName%" EQU "service" (
    ECHO ERROR - No service with the display name of "%serviceDisplayName%" was found.">> %logFile%
    GOTO :END
)


REM  ---------------------------------------
REM | *** OUTPUT INITIAL SERVICE STATUS *** |
REM  ---------------------------------------

ECHO Service Display Name:  %serviceDisplayName%>> %logFile%
ECHO Service Key Name:      %serviceKeyName%>> %logFile%

ECHO. >> %logFile%
ECHO [%time%] Initial Status:>> %logFile%
ECHO --------------------------->> %logFile%

SC QC %serviceKeyName%>>%logFile%
SC QUERY %serviceKeyName%>>%logFile%


REM  ---------------------------------------
REM | *** SET START MODE TO 'Automatic' *** |
REM  ---------------------------------------

ECHO. >> %logFile%
ECHO [%time%] Setting start mode to 'auto'...>> %logFile%

SC CONFIG %serviceKeyName% start= auto


REM  ---------------------------
REM | *** START THE SERVICE *** |
REM  ---------------------------

ECHO. >> %logFile%
ECHO [%time%] Attempting to start the service...>> %logFile%

SC START %serviceKeyName%

SLEEP %waitSeconds%

REM  -------------------------------
REM | *** OUTPUT SERVICE STATUS *** |
REM  -------------------------------

ECHO. >>%logFile%
ECHO [%time%] Final Status:>>%logFile%
ECHO -------------------------->> %logFile%

SC QC %serviceKeyName%>>%logFile%
SC QUERY %serviceKeyName%>>%logFile%

ECHO. >> %logFile%
ECHO [%time%] Script Complete>> %logFile%

1 个答案:

答案 0 :(得分:0)

您需要将大部分代码放在一个块中,并将%serviceKeyName%更改为%1

:<label>

your existing code

exit

然后像上面那样调用上面的

将label和servicename替换为任何内容。

call <label> <servername>

请参阅goto:eof(转到/?)并调用/?