1

我有三个服务(“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%
4

1 回答 1

0

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

:<label>

your existing code

exit

然后像这样调用上面的

用任何东西替换标签和服务名称。

call <label> <servername>

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

于 2013-10-17T00:07:07.180 回答