Denis.A
Denis.A

Reputation: 191

Powershell stopping services with dependencies

cannot understand why I can't stop a service with the powershell 'stop-service' as I can stop it with the 'net stop' command.

The service has one dependent service which is stopped (not a synchronization problem, I've tested 10 minutes after having stopped the dependent service).

Sorry for the french version of output...

PS C:\> stop-service pgsql-8.3
Stop-Service : Impossible d'arrêter le service « PostgreSQL Database Server 8.3 (pgsql-8.3) », car d'autres services en dépendent. Il peut être arrêté uniquement si l'indicateur Force est défini.
Au niveau de ligne : 1 Caractère : 13
+ stop-service <<<<  pgsql-8.3
    + CategoryInfo          : InvalidOperation: (System.ServiceProcess.Service
   Controller:ServiceController) [Stop-Service], ServiceCommandException
    + FullyQualifiedErrorId : ServiceHasDependentServices,Microsoft.PowerShell
   .Commands.StopServiceCommand

PS C:\> net stop pgsql-8.3
Le service PostgreSQL Database Server 8.3 s'arrête.
Le service PostgreSQL Database Server 8.3 a été arrêté.

The only way suggested by powershell is to force the stop. Is there a way to avoid this behaviour ?

Thanks in advance.

Denis

Upvotes: 1

Views: 6361

Answers (1)

Karl Fasick
Karl Fasick

Reputation: 53

Recommended solution

Docs state that -Force is used for stopping a service and DependentServices

Stop-Service

Stop-Service 'pgsql-8.3' -Force

Alternate solution

Pipe the DependentServices to Stop-Service to stop them first

Get-Service 'pgsql-8.3' -DependentServices | Stop-Service -Verbose

Get the DependentServices again to check they stopped

Get-Service 'pgsql-8.3' -DependentServices

Now the service should stop without -Force

Stop-Service 'pgsql-8.3' -Verbose

Starting should also start DependentServices

Start-Service 'pgsql-8.3' -Verbose

Upvotes: 1

Related Questions