11

When I run the following command:

Set-Service -ComputerName appserver -Name MyService -Status Stopped

I get an error message:

Set-Service : Cannot stop service 'My Service (MyService)' because it is
dependent on other services.
At line:1 char:12
+ Set-Service <<<<  -ComputerName appserver -Name MyService -Status Stopped
    + CategoryInfo          : InvalidOperation: (System.ServiceProcess.ServiceController:ServiceController) [Set-Service], ServiceCommandException
    + FullyQualifiedErrorId : ServiceIsDependentOnNoForce,Microsoft.PowerShell.Commands.SetServiceCommand

I can stop the service from the services.msc GUI, and I can start the service with Set-Service, but I can't stop it again.

It is true that the service depends on some other services, but I don't understand why that would prevent me from stopping it—nothing else depends on it.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Wolfgang
  • 3,470
  • 1
  • 20
  • 36

2 Answers2

19

The Set-Service cmdlet is for changing the configuration of a service. That you can use it to stop a service by changing its status is just coincidental. Use the Stop-Service cmdlet for stopping services. It allows you to stop dependent services as well via the parameter -Force. You'll need to retrieve the service object with Get-Service first, though, since Stop-Service doesn't have a parameter -ComputerName.

Get-Service -Computer appserver -Name MyService | Stop-Service -Force
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    [The documentation](https://technet.microsoft.com/en-us/library/hh849849.aspx) specifically states that Set-Service can stop and start services, so I still think this is a bug. But it is good to know that there is a straightforward workaround. – Harry Johnston Oct 01 '16 at 22:58
  • I didn't say that it can't. But just the service itself. If that isn't possible due to dependent services it doesn't allow passing the stop signal to them like `Stop-Service` does. Which was specifically made for stopping services. So I wouldn't consider this a bug in `Set-Service`. – Ansgar Wiechers Oct 01 '16 at 23:05
  • But there are no dependent services in this case. The service the OP is trying to stop is dependent on other services, not the other way around. – Harry Johnston Oct 01 '16 at 23:13
  • 1
    I did some more research and came across [this TechNet article](https://blogs.technet.microsoft.com/core/2012/01/06/kurz-erwhnt-powershell-set-service-dienst-terminierung-mit-abhngigen-diensten-schlgt-fehl/) which describes this exact issue (only in German unfortunately). According to this article `Set-Service` does not handle dependencies (in either direction), so `-Status Stopped` can only be used on services without dependencies. – Ansgar Wiechers Oct 01 '16 at 23:26
  • A curious way of putting it! As I see it, enumerating services that depend on the target service (and refusing to attempt to stop the service if there are any) makes sense. Enumerating the services that the target service depends on (and refusing to stop the service if there are any) is a bug, because that information is irrelevant to the operation. The cmdlet doesn't need to "handle" that case, it just needs to not bother fetching that information in the first place! – Harry Johnston Oct 01 '16 at 23:37
  • ... my best guess as to how that happened is that they're using the same logic to check dependencies regardless of whether the service is being stopped or started. Looked at from that perspective, you could call it a missing feature rather than a bug I guess. :-) – Harry Johnston Oct 01 '16 at 23:39
  • Unfortunately this tries to stop the service on the *local* computer! Apparently `Stop-Service` just looks at the name of the service it's passed with no regard for which computer it's on. – Wolfgang Oct 02 '16 at 04:58
  • @Wolfgang I just double-checked and cannot confirm. If `Get-Service` returns a remote `ServiceController` object, `Stop-Service` operates on that remote service. If you see different behavior: please provide evidence to the contrary. – Ansgar Wiechers Oct 02 '16 at 11:11
  • @AnsgarWiechers I do `Get-Service -Computer appserver -Name MyService` and get the service object, but then I `| Stop-Service` and get the error `Stop-Service : Cannot find any service with service name 'MyService'.` – Wolfgang Oct 02 '16 at 18:15
  • Show, don't tell. Full commandlines. Full output/error message. In your question. – Ansgar Wiechers Oct 02 '16 at 19:11
-2

I eventually resolved this problem with the following code, which calls sc to stop the service and then waits for it to finish stopping. This achieves the same result as expected from Set-Service -Status Stopped; that is, when it returns the service has been stopped. (sc on its own starts to stop the service, but does not wait until it has finished stopping.)

Start-Process "$env:WINDIR\system32\sc.exe" \\APPSERVER,stop,MyService -NoNewWindow -Wait
while ((Get-Service -ComputerName APPSERVER -Name MyService | 
Select -ExpandProperty Status) -ne 'Stopped') {
    Write-Host "Waiting for service to stop..."
    Start-Sleep -Seconds 10
}
Wolfgang
  • 3,470
  • 1
  • 20
  • 36