简体   繁体   English

Set-Service:无法停止服务,因为它依赖于其他服务

[英]Set-Service: Cannot stop service because it is dependent on other services

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. 我可以从services.msc GUI停止服务,我可以使用Set-Service 启动 Set-Service ,但我不能再停止它。

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. 确实,服务取决于其他一些服务,但我不明白为什么这会阻止我停止它 - 没有其他依赖它。

The Set-Service cmdlet is for changing the configuration of a service. Set-Service cmdlet用于更改服务的配置。 That you can use it to stop a service by changing its status is just coincidental. 您可以通过更改其状态来使用它来停止服务,这简直就是巧合。 Use the Stop-Service cmdlet for stopping services. 使用Stop-Service cmdlet停止服务。 It allows you to stop dependent services as well via the parameter -Force . 它允许您通过参数-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检索服务对象,因为Stop-Service没有参数-ComputerName

Get-Service -Computer appserver -Name MyService | Stop-Service -Force

I eventually resolved this problem with the following code, which calls sc to stop the service and then waits for it to finish stopping. 我最终使用以下代码解决了这个问题,该代码调用sc来停止服务,然后等待它完成停止。 This achieves the same result as expected from Set-Service -Status Stopped ; 这实现了与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.) sc自己开始停止服务,但不要等到它完成停止。)

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM