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

时间:2016-10-01 20:45:03

标签: powershell windows-services windows-server-2008-r2

当我运行以下命令时:

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 启动服务,但我无法再停止服务。

该服务确实取决于其他一些服务,但我不明白为什么会阻止我停止它 - else 依赖于它。

2 个答案:

答案 0 :(得分:8)

Set-Service cmdlet用于更改服务的配置。您可以通过更改其状态来使用它来停止服务,这简直就是巧合。使用Stop-Service cmdlet停止服务。它允许您通过参数-Force停止相关服务。但是,您需要首先使用Get-Service检索服务对象,因为Stop-Service没有参数-ComputerName

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

答案 1 :(得分:0)

我最终使用以下代码解决了这个问题,该代码调用sc来停止服务,然后等待它完成停止。这与Set-Service -Status Stopped的预期结果相同;也就是说,当它返回时服务已经停止。 (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
}