Stop a Windows service that depends on another service through a batch file

All,

I am trying to stop the created Windows service, which depends on another service. I just want to stop both services using a batch file, such as the sc command, where the services are running on a remote machine.

I tried stopping the services in a dependent order (least dependent first), but this does not stop the service.

For example, Service1 depends on Service2, which is configured in the Service settings in the Services console. I am running a script on my Windows 7 PC, and Windows Server 2003 is running on the server.

The following lines are in the batch file I create:

sc \\SERVER stop "Service1"
sc \\SERVER stop "Service2"

The output in the command console is:

D:\Test>sc \\SERVER stop "Service2"
[SC] ControlService FAILED 1051:

A stop control has been sent to a service that other running services are dependent on.

Service2 will not stop. Service1 stops working.

Any ideas?

Thank,

Andez

+5
4

"net stop" , . / .

, ,

net stop spooler /yes
+6

, , sc qc <service>

, script , for

:

@echo off
setlocal enabledelayedexpansion
set service=winmgmt
set server=server

for /f "Tokens=2 Delims=:" %%i in ('sc \\%server% qc %service% ^| find /i "DEPENDENCIES"') do (
    set depservice=%%i
    rem removes spaces
    set depservice=!depservice: =!
    sc \\%server% stop "!depservice!"

    rem extra: accumulate all dependencies to one variable
    set alldeps=!alldeps!, !depservice!
    rem remove first ", " in variable
    set alldeps=!alldeps=~2!

)
sc \\%server% stop "%service%" && echo Both %service% and !alldeps! were stopped || echo Something went wrong stopping %service%
exit /b

, , , .

+2

Niklas .

, Windows Server 2008 R2 qc , . , , .

, , , . EnumDepend sc.exe.

, , , .

+1

, :

, SC . :

?

+1

All Articles