Set-Service:无法停止服务,因为它依赖于其他服务 [英] Set-Service: Cannot stop service because it is dependent on other services

查看:45
本文介绍了Set-Service:无法停止服务,因为它依赖于其他服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下命令时:

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

我收到一条错误消息:

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

我可以从 services.msc GUI 停止服务,我可以使用 Set-Service启动服务,但我可以不要再阻止了.

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.

该服务确实依赖于其他一些服务,但我不明白为什么这会阻止我停止它——没有任何else依赖于它.

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.

推荐答案

我最终用下面的代码解决了这个问题,它调用 sc 来停止服务,然后等待它完成停止.这与 Set-Service -Status Stopped 的预期结果相同;也就是说,当它返回时,服务已停止.(sc 自己开始停止服务,但不会等到它完成停止.)

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
}

这篇关于Set-Service:无法停止服务,因为它依赖于其他服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆