Search code examples
powershellwindows-services

Service Cannot Be Stopped - Error stopping remote Winodws Service using Powershell


I have a simple script that stops/starts multiple services on remote server. It works fine but every other run I see following error. But even though it says it cannot stop the service actually it stopping it. I tried adding some pause time between different services but this doesn't seem to help.

This is how it's stopping the service

Get-Service -ComputerName SERVER1 -Name SERVICE1 | Stop-Service -Force

And this is full error

Stop-Service : Service 'SERVICE1 (SERVICE1)' cannot be stopped due to the following error: Cannot
stop SERVICE1 service on computer 'SERVER1'.
At C:\Script\dev1_stop.ps1:25 char:67
+ ... puterName SERVER1 -Name SERVICE1 | Stop-Service -Force
+                                                       ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Stop-Service],
   ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.StopServiceCommand

Solution

  • I would say that it is likely something specific to the service and/or its dependencies. Since I am unfamiliar with "SERVICE1", I can't really provide a better response. You might look in the System event log on the failed host(s).