Stop a service on Remote server
Posted: 13 Aug 2009 02:06
Hi All
I would like to stop a windows service remotely and wait until the service stops before proceeding to the next line of code. In other words, To write a batch code to stop services with dependencies, One service should stop completely before stopping the second one.
The code that i have written does stop the services but it is not waiting for the first service to stop before it stops the second one. Can someone please shed some light on this
thanks for your help
I would like to stop a windows service remotely and wait until the service stops before proceeding to the next line of code. In other words, To write a batch code to stop services with dependencies, One service should stop completely before stopping the second one.
The code that i have written does stop the services but it is not waiting for the first service to stop before it stops the second one. Can someone please shed some light on this
Code: Select all
@echo off
set server=servername
set winservice1=service1
set winservice2=service2
cls
echo Stopping service %winservice1% for server %server%...
REM STOP WINSERVICE1
sc \\%server% stop %winservice1% | findstr STATE
sc \\%server% query %winservice1% | findstr STATE | findstr STOPPED > NUL
if errorlevel 1 (
set output=Error while stopping service1 :(
goto end
)
echo Stopping service %winservice2% for server %server%...
REM STOP WINSERVICE2
sc \\%server% stop %winservice2% | findstr STATE
sc \\%server% query %winservice2% | findstr STATE | findstr STOPPED > NUL
if errorlevel 1 (
set output=Error while stopping service2 :(
goto end
)
goto end
:end
echo %output%
thanks for your help