[英]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
启动 Set-Service
,但我不能再停止它。
确实,服务取决于其他一些服务,但我不明白为什么这会阻止我停止它 - 没有其他依赖它。
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
所期望的相同的结果; 也就是说,当它返回时服务已经停止。 ( 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.