All,
I'm trying to stop a Windows service that we have created that is dependant on another service. I just want to stop both of the service using a batch file, sc command for example, where the services are running on a remote machine.
I have tried stopping the services in the order of dependancy (least dependant first), but it does not stop the service.
For example Service1 depends upon Service2 which is configured within the Service settings in the Services console. I am running the script on my Windows 7 PC and the server runs Windows Server 2003.
The following lines are in the noddy batch file I created:
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.
The service Service2 will not stop. Service1 stops fine.
Any ideas?
Thanks,
Andez
Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to disable a service and press Enter: sc config "SERVICE-NAME" start=disabled In the command, replace "SERVICE-NAME" for the name of the service that you want to disable.
Method 1: Using Command SC SC \computername STOP servicename. SC \computername START servicename.
Open the Service Control Manager by typing in the command window “services. msc”. Browse for the service you have modified, right click and select “Properties“. Select the “Dependency” tab.
The "net stop" command has a parameter which is not commented. This parameter is /yes and will automatically stop all the dependent services too
So to stop a service with or without dependencies you just type
net stop spooler /yes
You can check which dependencies a service has by running sc qc <service>
And in order to script that and retrieve the dependencies you can put it in a for-loop
Example:
@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
The above will only work if the service that you want to stop only has one dependency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With