batch to restart service remote server

November 16, 2011 at 16:35:04
Specs: N/A
hi i was wondering if any one could help me, i am trying to create a batch file that stops 3 services in a certain order then retarts them again on both the local host and a remote server.
i have been advised to create a argument to do this but iam not sure how.
this is what i have so far and it seems to work fine the problem i have now is creating the arguement to restart the services on another server.

echo off

rem location of script log
SET LOGPath="c:\output.txt"

rem How many seconds to wait in each loop cycle while waiting for a service to start
SET StartWaitLoopDelay=15

rem How many seconds to wait in each loop cycle while waiting for a service to stop
SET StopWaitLoopDelay=15


echo +===================================================+ > %LOGPath%
echo ^| Restarting WEB Services - %DATE% %TIME% ^| >> %LOGPath%
echo +===================================================+ >> %LOGPath%


echo Stopping the WEB services on LOCALHOST - %DATE% %TIME% >> %LOGPath%
sc stop service name >> %LOGPath%

echo Stopping the WEB services on LOCALHOST - %DATE% %TIME% >> %LOGPath%
sc stop service name >> %LOGPath%

echo Stopping the WEB services on LOCALHOST - %DATE% %TIME% >> %LOGPath%
sc stop service name >> %LOGPath%


:waitServiceStop
rem Wait a specified time between each check
timeout /t %StopWaitLoopDelay% /nobreak


sc \\localhost query service name | find /I "STATE" | find "STOPPED" > NUL
if errorlevel 1 goto waitServiceStop

sc \\localhost query service name | find /I "STATE" | find "STOPPED" > NUL
if errorlevel 1 goto waitServiceStop

sc \\localhost query service name | find /I "STATE" | find "STOPPED" > NUL
if errorlevel 1 goto waitServiceStop


echo Starting the WEB services on LOCALHOST - %DATE% %TIME% >> %LOGPath%
sc start service name >> %LOGPath%


echo Starting the WEB services on LOCALHOST - %DATE% %TIME% >> %LOGPath%
sc start service name >> %LOGPath%


echo Starting the WEB services on LOCALHOST - %DATE% %TIME% >> %LOGPath%
sc start service name >> %LOGPath%

echo. >> %LOGPath%
echo +============================================================+ >> %LOGPath%
echo ^| Finished Restarting WEB Services - %DATE% %TIME% ^| >> %LOGPath%
echo +============================================================+ >> %LOGPath%
echo. >> %LOGPath%
echo. >> %LOGPath%

echo on


See More: batch to restart service remote server

Report •

#1
November 16, 2011 at 17:20:09
I havent tested this myself but can you use the:
SC "\\SERVERNAME" stop servicename

This should communicate with the remote server and service. You might have to chuck in
RUNAS /user:[YOURDOMAIN]\youradminuser 
if you need the permission.

Also I had a similar problem I needed to solve on a remote server a few years ago. I ended up using

SCHTASKS
in the script to remotely create, run and delete tasks on the fly as they were needed.

I hope this helps.


Report •

#2
November 16, 2011 at 20:03:49
It did help, thank you very much :)

Report •
Related Solutions


Ask Question