@echo OFF
set LocalPath=%~dp0
cd /d "%LocalPath%"
:Setup logs
SET LOGFILE=%1_installservices.log
ECHO Start at %DATE%%TIME% >>%LOGFILE% 2>&1
ECHO Log is ready >>%LOGFILE% 2>&1
ECHO ******************************************************************** >>%LOGFILE% 2>&1
ECHO **** Start Install Services **************** >>%LOGFILE% 2>&1
ECHO ******************************************************************** >>%LOGFILE% 2>&1
set ServerName_1=\\%1
set BinPath="\"F:\PGSApp\PGSLogScanCenter\PGSLogScanCenter.exe\""
set UserName=
set Pwd=
set ServiceName="PGSLogScanCenter"
set "ServiceStatus=UNKNOWN"
call:checkServicesStatus %ServiceName% %ServerName_1% >>%LOGFILE% 2>&1
if "%ServiceStatus%" equ "RUNNING" (
sc %ServerName_1% stop %ServiceName% >>%LOGFILE% 2>&1
)
call:isServicesStopped %ServiceName% %ServerName_1% >>%LOGFILE% 2>&1
if "%ServiceStatus%" equ "STOPPED" (
sc %ServerName_1% delete %ServiceName% >>%LOGFILE% 2>&1
)
:: Make sure the services is deleted
call:checkServicesStatus %ServiceName% %ServerName_1% >>%LOGFILE% 2>&1
if "%ServiceStatus%" equ "UNKNOWN" (
sc %ServerName_1% create %ServiceName% binpath= %BinPath% start= auto obj= %UserName% password= %Pwd% >>%LOGFILE% 2>&1
sc %ServerName_1% start %ServiceName% >>%LOGFILE% 2>&1
)
ECHO ******************************************************************** >>%LOGFILE% 2>&1
ECHO **** End **************** >>%LOGFILE% 2>&1
ECHO ******************************************************************** >>%LOGFILE% 2>&1
ECHO End at %DATE%%TIME% >>%LOGFILE% 2>&1
ECHO ------------------------------------------------------------ >>%LOGFILE% 2>&1
echo.&goto:eof
:isServicesStopped -- loop to check the services stopped or not
:: -- %~1: Services name
:: -- %~2: Server name
call:checkServicesStatus %~1 %~2
if "%ServiceStatus%" neq "STOPPED" (
if "%ServiceStatus%" neq "UNKNOWN" (
timeout /t 5
call:isServicesStopped %~1 %~2
)
)
if "%ServiceStatus%" equ "STOPPED" (
set "ServiceStatus=STOPPED"
)
goto:eof
:checkServicesStatus -- Check the services status
:: -- %~1: Services name
:: -- %~2: Server name
set "inForLoop=false"
for /f "tokens=3 delims=: " %%p in ('sc %~2 query "%~1" ^| findstr "STATE"') do (
set "ServiceStatus=%%p"
set "inForLoop=true"
)
if "%inForLoop%" equ "false" (
set "ServiceStatus=UNKNOWN"
)
goto:eof
SC Services Operation , for loop ...
最新推荐文章于 2025-03-26 14:38:23 发布