At one of my clients, I'm running into an issue when using SC to STOP and START services.
The messgae I get is as follows:
OpenSCManager Failed 1722
My code is as follows:
Code: Select all
:EXEC_ACTION
PING -n 10 127.0.0.1>nul
IF [%1] == [] GOTO Usage
IF [%2] == [] GOTO Usage
IF [%3] == [] GOTO Usage
SET ACTION=%1
SET SERVICENAME=%2
SET SYSTEMNAME=%3
IF %ACTION%==stop ( SET ACTION=STOP)^
ELSE IF %ACTION%==STOP ( SET ACTION=STOP)^
ELSE IF %ACTION%==start ( SET ACTION=START)^
ELSE IF %ACTION%==START ( SET ACTION=START)^
ELSE IF %ACTION%==restart ( SET ACTION=RESTART)^
ELSE IF %ACTION%==RESTART ( SET ACTION=RESTART)^
ELSE GOTO Usage
SET STATE=
SET CURRENT_STATUS=
SET /A DEFAULT_DELAY=5
SET /A SLEEP_COUNT=0
SET /A RESTARTED=0
SET /A MAX_WAIT_PERIODS=5
ECHO Process Info: >>%logfile%
ECHO. >>%logfile%
ECHO Server Name : %SYSTEMNAME% >>%logfile%
ECHO Action : %ACTION% >>%logfile%
ECHO Service Name : %SERVICENAME% >>%logfile%
ECHO. >>%logfile%
ECHO Attempting to %ACTION% service %SERVICENAME% on computer %SYSTEMNAME% >>%logfile%
SC \\%SYSTEMNAME% query %SERVICENAME% | FIND "FAILED 1060" >nul 2>&1
IF %ERRORLEVEL%==0 IF NOT %ERRORLEVEL%==1 (
ECHO. >>%logfile%
ECHO Failure! Service %SERVICENAME% is not valid! >>%logfile%
EXIT /B 0
)
SC \\%SYSTEMNAME% query %SERVICENAME% | FIND "STATE" >nul 2>&1
IF %ERRORLEVEL%==1 IF NOT %ERRORLEVEL%==2 (
ECHO. >>%logfile%
ECHO Failure! Server \\%SYSTEMNAME% or service %SERVICENAME% is not accessible or is offline! >>%logfile%
EXIT /B 0
)
:Dispatch
FOR /f "tokens=*" %%i IN ('SC \\%SYSTEMNAME% query %SERVICENAME% ^| FIND "STATE"') DO SET STATE=%%i
ECHO %STATE% | FINDSTR /C:"1" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=STOPPED
ECHO %STATE% | FINDSTR /C:"2" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=START_PENDING
ECHO %STATE% | FINDSTR /C:"3" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=STOP_PENDING
ECHO %STATE% | FINDSTR /C:"4" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=RUNNING
ECHO %STATE% | FINDSTR /C:"5" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=CONTINUE_PENDING
ECHO %STATE% | FINDSTR /C:"6" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=PAUSE_PENDING
ECHO %STATE% | FINDSTR /C:"7" >nul
IF %ERRORLEVEL%==0 SET CURRENT_STATUS=PAUSED
ECHO Current status of service is %CURRENT_STATUS% >>%logfile%
IF NOT "%CURRENT_STATUS%"=="RUNNING" IF NOT "%CURRENT_STATUS%"=="STOPPED" IF NOT "%CURRENT_STATUS%"=="PAUSED" (
IF "%SLEEP_COUNT%"=="%MAX_WAIT_PERIODS%" (
ECHO Service state won't change. Script exececution is canceled.>>%logfile%
EXIT /B 0
)
ECHO Service State is changing, waiting %DEFAULT_DELAY% seconds...>>%logfile%
SLEEP %DEFAULT_DELAY%
SET /A SLEEP_COUNT+=1
GOTO Dispatch
)
IF %ACTION%==START (
IF %CURRENT_STATUS%==RUNNING (
ECHO. >>%logfile%
ECHO Service %SERVICENAME% is currently running. >>%logfile%
ECHO No further action is necessary >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
) ELSE (
GOTO StartService
)
) ELSE IF %ACTION%==RESTART (
IF %CURRENT_STATUS%==RUNNING (
IF %RESTARTED%==1 (
ECHO. >>%logfile%
ECHO Service %SERVICENAME% has been restarted. >>%logfile%
ECHO No further action is necessary >>%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 Service %SERVICENAME% is currently stopped. >>%logfile%
ECHO No further action is necessary >>%logfile%
ECHO. >>%logfile%
GOTO EndExit
) ELSE (
GOTO StopService
)
)
:StartService
ECHO Starting %SERVICENAME% on \\%SYSTEMNAME% >>%logfile%
SC \\%SYSTEMNAME% start %SERVICENAME% >>%logfile%
ECHO. >>%logfile%
ECHO Confirming service has been started.. >>%logfile%
ECHO. >>%logfile%
SET SLEEP_COUNT=0
GOTO Dispatch
:StopService
ECHO Stopping %SERVICENAME% on \\%SYSTEMNAME% >>%logfile%
SC \\%SYSTEMNAME% stop %SERVICENAME% >>%logfile%
ECHO. >>%logfile%
ECHO Confirming service has been stopped... >>%logfile%
ECHO. >>%logfile%
SET SLEEP_COUNT=0
GOTO Dispatch
However, if I hard code the values for the SC command, it works fine.
Code: Select all
SC \\SERVER1 stop "SERVICE NAME"
File and printer sharing on the remote computer has also been confirmed that it's enabled.
Could it be a Firewall issue?
Thanks!