Restart-service fails

I have a script that is trying to restart and service on a server. the script works on 3 out of 4 servers
I am running the script manually - for testing- as administrator with executionpolicy unrestricted.

The error message is cannot be stopped due to the following error: Cannot stop XXXX service on computer ‘.’

All 4 servers are configured in the same way - at least a far as can be known

Need some ideas on what the problem might be and what the fix is.

Bit challenging to assist with no code. Google led me to this:

powershell - Set-Service: Cannot stop service because it is dependent on other services - Stack Overflow

There is no additional context in the error message aside from cannot stop the service? Likely there is a processes that is hung up and there is a timeout occuring. Is there anything in the event logs?

The code is restart-service -name XXX

There are no dependencies and the code works happily on the 3 other services and everything, on the face of it, looks to be the same.

Just looking for some more in depth knowledge of things it could be that are not currently on the net, just that I’ve googled for many hours and tried all those results that I can find.

I can stop the service using windows, but need this to be automated.

Thanks

What happens with -Force ??

Have you double checked the name its looking for?
Example;

Restart-service -name SMS Agent host 

Will not work on some commands. Some are looking for the service name and not the display name.

Restart-Service -name ccmexec

Just throwing it out there as a possibility. If the service name and the display name dont match, try the other one.

You could also try
net stop *service name*
Then follow up with a net start service name if the stop command stops it.

The service exists on 4 servers, the code works on 3 of the 4, but not the forth. There is some, currently unknown, difference between the machines that I am hoping someone may be able to provide some information on that will help resolve my problem.

Same error. The code works on 3 out of 4 servers, so there must be something different within the servers (even though they were all built using a standard build process)

Is there are way to get the error to be more meaningful?
Can some diagnostics be added that identifies what is causing the code to break?

Hi,

This looks like a windows issue and not powershell but you can try:
Restart-Service -Name “ccmexec” -Verbose

Verbose usually shows more info about errors or completions.
Let us know what the output of the verbose is.

Als you can try this:
$Computer = “SERVER1” #change to your server4
$ServiceName = “ccmexec” #change to your service you want to restart
$Service = Get-Service -Name $ServiceName -ComputerName $Computer
Restart-Service -InputObj $Service -ErrorAction stop

Last and mostly:
Learn to use New-PsSession, this should always work when connecting to a remote machine.
As long winrm is working.

Thanks for the idea, but that also suffers the same problem

Adding verbose does not change the message output.

I agree that something within windows must be different, but with I am unable to proceed further without powershell, or the community, providing something that may lead me to understand and then fix.

so whats the message/error?
We are unable to help you if you don’t supply more info.

also, are you able to Get-Process from server4?

The exact error message is

Restart-Service : Service ‘XXX’ cannot be stooped due to the following error: Cannot stop XXX service on computer ‘YYY’.
At line:4 char:1

  • Restart-Service -InputObj $Service
  •     + CategoryInfo            : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandEXception
       + FullyQualifiedErrorId : CouldNotStopService,Microsft.Powershell.Commands.RestartServiceCommand
    
    

I can list all the services on the server. I haven’t got any others that I can easily stop to see if the problem is specific to this one service on the specific server.

Check your powershell version on that server?

All servers are 5.1.14393.5582

The account you are running is allowed to remote powershell because you can get all services.

01.) Did you start powershell as admin when running the restart-service?
02.) Check the account that runs the service, use those credentials to restart the service via Invoke-Command.
03.) Just to be sure, give us the result from Get-ExecutionPolicy.