Wol*_*ang 4 powershell windows-services windows-server-2008-r2
当我运行以下命令时:
Set-Service -ComputerName appserver -Name MyService -Status Stopped
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
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.mscGUI 停止服务,我可以启动服务Set-Service,但我无法再停止它.
确实,服务取决于其他一些服务,但我不明白为什么这会阻止我停止它 - 没有其他依赖它.
该Set-Servicecmdlet用于更改服务的配置.您可以通过更改其状态来使用它来停止服务,这简直就是巧合.使用Stop-Servicecmdlet停止服务.它允许您通过参数停止依赖服务-Force.但是,您需要首先检索服务对象Get-Service,因为Stop-Service它没有参数-ComputerName.
Get-Service -Computer appserver -Name MyService | Stop-Service -Force
Run Code Online (Sandbox Code Playgroud)
Wol*_*ang -2
我最终使用以下代码解决了这个问题,该代码调用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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10069 次 |
| 最近记录: |