IF conditional making code fail - why?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

IF conditional making code fail - why?

#1 Post by SIMMS7400 » 20 Nov 2016 20:03

Hi Folks -

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!

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: IF conditional making code fail - why?

#2 Post by SIMMS7400 » 20 Nov 2016 20:09

Instead of:

Code: Select all

 IF [%RSFLAG] == [] GOTO SRVC


I used
:

Code: Select all

IF NOT %~1==restart_server GOTO SRVC


and it's working as expected.

Still confused as to why the former wouldn't work though?

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: IF conditional making code fail - why?

#3 Post by SIMMS7400 » 20 Nov 2016 20:21

Please ignore - it was set wrong.

I'm sorry to take anyone's time.

Thanks!

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: IF conditional making code fail - why?

#4 Post by Squashman » 20 Nov 2016 20:22

Maybe because you initialized the variable to never be empty.

Code: Select all

SET /A RSFLAG=0

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: IF conditional making code fail - why?

#5 Post by SIMMS7400 » 21 Nov 2016 08:21

Exactly!

Also Squash -

I's obviously something I did, because this was working yesterday fine. But, if I pass in a quoted string with spaces to my script, it will fails and says impropper syntax.

Code: Select all

::-- Set Working Directory as Script Path --::

cd /d %~dp0

::-- Call Environment File --::

call _env.cmd

::-- Set EPM Services process specific variables --::

SET PLOGPATH=EPM_Services_Logs\
SET PERRORPATH=EPM_Services_Errors\

::-- Set process Action --::
::-- Options : stop ^| start ^| restart ^| restart_server

SET ACTION=stop

::-- Set Servers --::

SET RPT_SRVR=DIDRHYPRPT03VW
SET PLN_SRVR=DIDRHYPRPT03VW
SET ESSB_SRVR=DIDRHYPESS03VW

echo ********************************************************
echo Stop EPM Services on Reporting and Planning Server                              
echo ********************************************************

FOR %%S IN (
 "OracleProcessManager_ohsInstance4262192120"^
 "HyS9FoundationServices"^
 "HyS9RaFramework"^
 "HyS9RAFrameworkAgent"^
 "HyS9FRReports"^
 "HyS9FRPrint"^
 "HyS9WebAnalysis"^
 "Hyperion RMI Registry"^
 "HyS9Planning"
 ) DO (

CALL SafeStopStartRestart.cmd %ACTION% %%S %RPT_SRVR% %PLOGPATH% %PERRORPATH%

)


It fails on Hyperion RMI Registry. I'm not sure why?


SafeStopStartRestart.cmd

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)

::-- Append server name to log ^& error files --::
IF %~2==Restart_Server (
   SET logfile=%intrapath%%1_%3_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log
   SET errorfile=%errorintrapath%%1_%3_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log
   GOTO 1
   )

::-- Append service name to log ^& error files --::
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

:1

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

ECHO %ACTION%
ECHO %SERVICENAME%
ECHO %SYSTEMNAME%
TIMEOUT /T 2

::-- 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 RSFLAG=

SET /A DEFAULT_DELAY=10
SET /A SLEEP_COUNT=0
SET /A RESTARTED=0
SET /A MAX_WAIT_PERIODS=5
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
   )

::-- If Server related, set Server to NOT APPLICABLE --::
::-- Set RSFLAG==T so server processes only against relevant code --:
::-- Find IP Address and Set as Variable --::

IF %~2==Restart_Server (
   SET SERVICENAME=NOT_APPLICABLE
   SET RSFLAG=T
   FOR /f "delims=[] tokens=2" %%a IN ('ping %SYSTEMNAME% -n 1 ^| findstr "["') DO SET IP=%%a
   )

::-- Display content to CLI for manual execution --::
CLS
ECHO ************************************************************************
ECHO Process Info  : %TIME%                                                         
ECHO ------------------------------------------------------------------------                                                               
ECHO Server Name   : %SYSTEMNAME%
IF [%IP%] NEQ [] (
ECHO IP Address    : %IP%
)
ECHO.                                             
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%
IF [%IP%] NEQ [] (
ECHO IP Address    : %IP%                                          >>%logfile%
)
ECHO.                                                                          >>%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 --::

::-- If service related, skip over below code as it only pertains to servers --::
IF [%RSFLAG%] == [] GOTO SRVC

ping -n 1 %SYSTEMNAME% | FIND /i "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 /i "Destination host unreachable" >nul 2>&1

IF %ERRORLEVEL%==0 (
   ECHO.                                                           >>%logfile%
   ECHO %SYSTEMNAME% is unreachable ^: No valid route to the requested host   >>%logfile%
   ECHO Will try to contact host %MAX_WAIT_PERIODS% more 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
   )

:P3
ping -n 1 %SYSTEMNAME% | FIND /i "Request timed out" >nul 2>&1

IF %ERRORLEVEL%==0 (
   ECHO.                                                              >>%logfile%
   ECHO The request to %SYSTEMNAME% has timed out                                >>%logfile%
   ECHO No ECHO reply messages were recieved within the default time of 1 second >>%logfile%
   ECHO.                                                              >>%logfile%
   ECHO Will try to contact host %MAX_WAIT_PERIODS% more 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 P3
   )

ping -n 1 %SYSTEMNAME% | FIND /i "could not find host">nul 2>&1

IF %ERRORLEVEL%==0 (
   ECHO.                                                  >>%logfile%
   ECHO %SYSTEMNAME% is not valid : No further attempts will be made >>%logfile%
   ECHO.                                                  >>%logfile%
   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


I'm put echo throughout to test. it's not even getting passed to SafeStopStartRestart and I'm not sure why.

It fails and says "RMI was unexpected at this time".

Can you see anything wrong?

Thanks!

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: IF conditional making code fail - why?

#6 Post by thefeduke » 21 Nov 2016 11:27

The quotes on your strings are user to define the strings to IN ("..." "...") in the FOR loop, but I think that the variable is assigned the string itself. Try:
Suggested code wrote:CALL SafeStopStartRestart.cmd "%ACTION%" %%S %RPT_SRVR% %PLOGPATH% %PERRORPATH%

John A.

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: IF conditional making code fail - why?

#7 Post by SIMMS7400 » 21 Nov 2016 12:18

Hi John -

Thank you for the reply! Unfortunately, that didn't solve it. Also, the issue is with the 2nd command line argument, not the first (%ACTION%).

Thanks!

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: IF conditional making code fail - why?

#8 Post by thefeduke » 21 Nov 2016 12:32

SIMMS7400 wrote:Hi John -

Thank you for the reply! Unfortunately, that didn't solve it. Also, the issue is with the 2nd command line argument, not the first (%ACTION%).

Thanks!
Of, course! The old guy is losing it again. Try quoting the "%%S", instead.

John A.

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: IF conditional making code fail - why?

#9 Post by SIMMS7400 » 21 Nov 2016 13:02

Hi John-

Thanks for the reply again. That was one of the first things I tried :/

Post Reply