I've built a script that will safley stop, start, or restart services AND servers.
However, there is a section of code that only pertains to server. This code just cycles through various ping commands checking for return language. Therefore, if the process pertain to a Server action, a variable RSFLAG gets set to 1. Therefore, at the top of the code that pertains to the Server section, I put a simple if condition so if its NOT server related, the process will jump over it:
For instance:
Code: Select all
::-- Skip over the below section as it only pertains to Servers --::
IF [%RSFLAF%] == [] GOTO SRVC
ping -n 1 %SYSTEMNAME% | FIND "TTL=" >nul 2>&1
IF %ERRORLEVEL%==0 (
ECHO. >>%logfile%
ECHO Server %SYSTEMNAME% is valid and available for processing >>%logfile%
ECHO. >>%logfile%
GOTO RestartServer
)
:P2
ping -n 1 %SYSTEMNAME% | FIND "Destination host unreachable" >nul 2>&1
IF %ERRORLEVEL%==0 (
ECHO. >>%logfile%
ECHO %SYSTEMNAME% is unreachable ^: Will try to contact host %MAX_WAIT_PERIODS% times >>%logfile%
ECHO. >>%logfile%
IF "%PING_CNT%"=="%MAX_WAIT_PERIODS%" (
ECHO. >>%logfile%
ECHO %SYSTEMNAME% state won't change. Script exececution is canceled >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
)
PING -n %DEFAULT_DELAY% 127.0.0.1>nul
SET /A PING_CNT+=1
GOTO P2
)
ping -n 1 %SYSTEMNAME% | FIND /i "could not find host">nul 2>&1
ECHO %ERRORLEVEL%
IF %ERRORLEVEL%==0 (
ECHO. >>%logfile%
ECHO %SYSTEMNAME% is not valid : No further attempts will be made >>%logfile%
ECHO. >>%logfile%
ECHO not valid
PAUSE
GOTO EndExit
)
:SRVC
However, with that IF statement, the code below it never gets acted upon. How come?
Here is my full script as well:
Code: Select all
::-- Set Working Directory as Script Path --::
cd /d %~dp0
::-- Call Environment File --::
call _env.cmd
::-- Set Main Intrapath Variables --::
::-- Set Process in variables below i.e. EPMA_Logs\
SET PLOGPATH=%4
SET PERRORPATH=%5
SET intrapath=%MAINPATH%%LOGPATH%%PLOGPATH%
SET errorintrapath=%MAINPATH%%ERRORPATH%%PERRORPATH%
IF NOT EXIST %intrapath% MKDIR %intrapath%
IF NOT EXIST %errorintrapath% MKDIR %errorintrapath%
::-- Prepare Main Log and Error Files --::
FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)
SET logfile=%intrapath%%1_%2_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log
SET errorfile=%errorintrapath%%1_%2_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log
IF EXIST %logfile% DEL %logfile%
IF EXIST %errorfile% DEL %errorfile%
::-- Call function to pass in ACTION ^| SERVICE ^| SERVER --::
CALL :EXEC_ACTION %1 %2 %3
::-- Return to main script to process more commands or exit session --::
EXIT /B
::::::::::::::::::::::::::::::
::-- Functions Below Here --::
::::::::::::::::::::::::::::::
:EXEC_ACTION
::-- Ensure command line argurement exist before processing --%
IF [%1] == [] GOTO Usage
IF [%2] == [] GOTO Usage
IF [%3] == [] GOTO Usage
::-- Set command line arguements to a variable for easier reading --::
::-- Perform variable transformation --::
SET ACTION=%~1
SET SERVICENAME=%2
SET SYSTEMNAME=%~3
::-- Uppercase action values --::
SET "str=%ACTION%"
FOR /f "usebackq delims=" %%I IN (`powershell "\"%str%\".toUpper()"`) DO SET "ACTION=%%~I"
::-- Set Main Script Variables --::
SET STATE=
SET CURRENT_STATUS=
SET /A DEFAULT_DELAY=10
SET /A SLEEP_COUNT=0
SET /A RESTARTED=0
SET /A MAX_WAIT_PERIODS=5
SET /A RSFLAG=0
SET /A PING_CNT=0
::-- Set Ping Delay based on Action --::
IF %~1==stop (
SET /A PING_DELAY=10
) ELSE IF %~1==STOP (
SET /A PING_DELAY=10
) ELSE IF %~1==restart_server (
SET /A PING_DELAY=10
) ELSE (
SET /A PING_DELAY=60
)
::-- Service name is not applicable to server restarts --::
IF %~2==Restart_Server (
SET SERVICENAME=NOT_APPLICABLE
SET RSFLAG=1
)
::-- Used to shorten command line --::
SET PSSERVICE=PsService.exe
::-- Display content to CLI for manual execution --::
CLS
ECHO ************************************************************************
ECHO Process Info : %TIME%
ECHO ------------------------------------------------------------------------
ECHO Server Name : %SYSTEMNAME%
ECHO Action : %ACTION%
ECHO Service Name : %SERVICENAME:"=%
ECHO.
IF [%RSFLAG%] == [] (
ECHO Attempting to %ACTION% service %SERVICENAME% on computer %SYSTEMNAME%
) ELSE (
ECHO Attempting to %ACTION% : %SYSTEMNAME%
)
ECHO ------------------------------------------------------------------------
::-- Delay execution based on Action --::
PING -n %PING_DELAY% 127.0.0.1>nul
ECHO ************************************************************************ >>%logfile%
ECHO Process Info : %TIME% >>%logfile%
ECHO ------------------------------------------------------------------------ >>%logfile%
ECHO Server Name : %SYSTEMNAME% >>%logfile%
ECHO Action : %ACTION% >>%logfile%
ECHO Service Name : %SERVICENAME:"=% >>%logfile%
ECHO. >>%logfile%
IF [%RSFLAG%] == [] (
ECHO Attempting to %ACTION% service %SERVICENAME% on computer %SYSTEMNAME% >>%logfile%
) ELSE (
ECHO Attempting to %ACTION% : %SYSTEMNAME% >>%logfile%
)
ECHO ------------------------------------------------------------------------ >>%logfile%
::-- Ensure Service and^/or Server is valid, accessiable, and not offline --::
::-- Skip over the below section as it only pertains to Servers --::
IF [%RSFLAF%] == [] GOTO SRVC
ping -n 1 %SYSTEMNAME% | FIND "TTL=" >nul 2>&1
IF %ERRORLEVEL%==0 (
ECHO. >>%logfile%
ECHO Server %SYSTEMNAME% is valid and available for processing >>%logfile%
ECHO. >>%logfile%
GOTO RestartServer
)
:P2
ping -n 1 %SYSTEMNAME% | FIND "Destination host unreachable" >nul 2>&1
IF %ERRORLEVEL%==0 (
ECHO. >>%logfile%
ECHO %SYSTEMNAME% is unreachable ^: Will try to contact host %MAX_WAIT_PERIODS% times >>%logfile%
ECHO. >>%logfile%
IF "%PING_CNT%"=="%MAX_WAIT_PERIODS%" (
ECHO. >>%logfile%
ECHO %SYSTEMNAME% state won't change. Script exececution is canceled >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
)
PING -n %DEFAULT_DELAY% 127.0.0.1>nul
SET /A PING_CNT+=1
GOTO P2
)
ping -n 1 %SYSTEMNAME% | FIND /i "could not find host">nul 2>&1
ECHO %ERRORLEVEL%
IF %ERRORLEVEL%==0 (
ECHO. >>%logfile%
ECHO %SYSTEMNAME% is not valid : No further attempts will be made >>%logfile%
ECHO. >>%logfile%
ECHO not valid
PAUSE
GOTO EndExit
)
:SRVC
%PSSERVICE% \\%3 query %SERVICENAME% | FIND "FAILED 1060" >nul 2>&1
IF %ERRORLEVEL%==0 (
ECHO. >>%logfile%
ECHO Failure! Service %SERVICENAME% is not valid! >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
)
%PSSERVICE% \\%3 query %SERVICENAME% | FIND "STATE" >nul 2>&1
IF %ERRORLEVEL%==1 (
ECHO. >>%logfile%
ECHO Failure! Server \\%3 or service %SERVICENAME% is not accessible or is offline! >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
)
:Dispatch
::-- Parse output of command and set STATE as a variable for script processing --::
FOR /f "tokens=4" %%S IN ('%PSSERVICE% \\%3 query %SERVICENAME% ^| FIND "STATE"') DO SET CURRENT_STATUS=%%S
ECHO Current State : Service is %CURRENT_STATUS% >>%logfile%
::-- Perform required checks and balances after service has been stopped or started --::
IF NOT "%CURRENT_STATUS%"=="RUNNING" IF NOT "%CURRENT_STATUS%"=="STOPPED" IF NOT "%CURRENT_STATUS%"=="PAUSED" (
IF "%SLEEP_COUNT%"=="%MAX_WAIT_PERIODS%" (
ECHO. >>%logfile%
ECHO Service state won't change. Script exececution is canceled.>>%logfile%
ECHO. >>%logfile%
GOTO EndExit
)
ECHO. >>%logfile%
ECHO Service State is changing, waiting %DEFAULT_DELAY% seconds...>>%logfile%
ECHO. >>%logfile%
PING -n %DEFAULT_DELAY% 127.0.0.1>nul
SET /A SLEEP_COUNT+=1
GOTO Dispatch
)
IF %ACTION%==START (
IF %CURRENT_STATUS%==RUNNING (
ECHO. >>%logfile%
ECHO Warning! Service %SERVICENAME% is already running >>%logfile%
ECHO. >>%logfile%
ECHO No further processing required >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
) ELSE (
GOTO StartService
)
) ELSE IF %ACTION%==RESTART (
IF %CURRENT_STATUS%==RUNNING (
IF %RESTARTED%==1 (
ECHO. >>%logfile%
ECHO Warning! Service %SERVICENAME% has already been restarted >>%logfile%
ECHO. >>%logfile%
ECHO No further processing required >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
)
SET /A SLEEP_COUNT=0
GOTO StopService
) ELSE (
SET /A RESTARTED=1
GOTO StartService
)
) ELSE IF %ACTION%==STOP (
IF %CURRENT_STATUS%==STOPPED (
ECHO. >>%logfile%
ECHO Warning! Service %SERVICENAME% is already stopped. >>%logfile%
ECHO. >>%logfile%
ECHO No further processing required >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
) ELSE (
GOTO StopService
)
)
:StartService
::-- Ensure conlficts are captured and addressed appropriately --::
::-- For instance, a service could have a status of STOPPED and an action of START --::
::-- When SC or PsService goes to start it, it fails because infact it's already started --::
ECHO Starting %SERVICENAME% on \\%SYSTEMNAME% >>%logfile%
SET FND_STRNG=Error
%PSSERVICE% \\%3 start %SERVICENAME% |findstr /C:"%FND_STRNG%" >>%logfile%
IF %ERRORLEVEL%==0 (
ECHO. >>%logfile%
ECHO Warning! A service conflicts exists: >>%logfile%
ECHO. >>%logfile%
ECHO %~2 returned a %CURRENT_STATUS% status >>%logfile%
ECHO The action was to %ACTION% the service >>%logfile%
ECHO PsService FAILED because it was already STARTED >>%logfile%
ECHO. >>%logfile%
ECHO Please verify the incorrect status setting >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
)
ECHO. >>%logfile%
ECHO Verifying service has been started...>>%logfile%
ECHO. >>%logfile%
SET SLEEP_COUNT=0
GOTO Dispatch
:StopService
ECHO. >>%logfile%
ECHO Stopping %SERVICENAME% on \\%SYSTEMNAME% >>%logfile%
%PSSERVICE% \\%3 stop %SERVICENAME% >>%logfile%
ECHO. >>%logfile%
ECHO Verifying service has been stopped... >>%logfile%
ECHO. >>%logfile%
SET SLEEP_COUNT=0
GOTO Dispatch
:RestartServer
ECHO Restarting %SYSTEMNAME% at %TIME% >>%logfile%
CALL %WINDIR%\system32\shutdown -m \\%3 -r -t 00 ||GOTO FAILED
ECHO. >>%logfile%
ECHO %SYSTEMNAME% is being restarted : Allow 5 minutes to restart >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
:FAILED
ECHO. >>%logfile%
ECHO Warning! Access is denied : Server can not be restarted >>%logfile%
ECHO. >>%logfile%
ECHO Contact your System Administrator >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
:Usage
>>%logfile%(
ECHO.
ECHO Warning! EPM Services processing has been aborted
ECHO.
ECHO %1,%2, or %3 has been provided in an incorrect format
ECHO.
ECHO See usage below:
ECHO.
ECHO This script can start/stop/restart a local or remote service, waiting for the service to stop/start ^(if necessary^).
ECHO.
ECHO Usage:
ECHO ^*_EPM_Services.cmd ^<start^|stop^|restart^> ^<SERVICE^> ^[SYSTEM^]
ECHO.
)
GOTO EndExit
:EndExit
ECHO ************************************************************************>>%logfile%
ECHO ALERT : Server %3 is done being processed - Exiting Server >>%logfile%
ECHO ------------------------------------------------------------------------>>%logfile%
ECHO.
GOTO :EOF
Thank you!