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

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

当我运行以下命令时:

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 没有任何依赖 它.

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

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

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