Jenkins_StopService

@ECHO OFF

IF %1]==] GOTO Usage

FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ("%1") DO SET @DependedServer=%%A&SET @DependedService=%%B
IF ERRORLEVEL 1 (
   ECHO ERROR: ** The input parameters - %1 init failed
   GOTO ExitError
)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO ::::: Check and stop depended service -   %@DependedServer%/%@DependedService%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

ECHO INFO : ## Check depended service - %@DependedServer%/%@DependedService% state ...
SC \\%@DependedServer% QUERY %@DependedService% | FIND /I "STATE" | FIND "STOPPED"
IF ERRORLEVEL 1 GOTO StopService
GOTO ServiceStopped

:StopService
ECHO INFO : ## Stop depended service - %@DependedServer%/%@DependedService% ...
SC \\%@DependedServer% STOP %@DependedService%
IF ERRORLEVEL 1 (
   ECHO ERROR: ** Could not stop related service - %@DependedServer%/%@DependedService%
   GOTO ExitError
)

:StopServiceLoop
::Wait for related service to stop, sleeping for 1 second between checks
ECHO INFO : ## Waiting for service - %@DependedServer%/%@DependedService% to stop...
PING 127.0.0.1 -n 2 > nul
SC \\%@DependedServer% QUERY %@DependedService% | find /I "STATE" | find "STOPPED"
IF ERRORLEVEL 1  GOTO StopServiceLoop

::Wait an extra 2 seconds for the system to settle down so we can deploy our applications.
PING 127.0.0.1 -n 4 > nul

:ServiceStopped
ECHO INFO : ## Stop depended service - %@DependedServer%/%@DependedService% successfully
EXIT /B 0

:ExitError
ECHO Error : ** Stop depended service - %@DependedServer%/%@DependedService% failed
EXIT /B 1

:Usage
ECHO Usage: %0 DependedServer/DependedService
ECHO Example: %0 SZGFDEVAP11/DMWatchService

原文地址:https://www.cnblogs.com/flysun0311/p/2624696.html