【问题标题】:Powershell Stop Services, defrg disks and then start servicesPowershell停止服务,defrg磁盘然后启动服务
【发布时间】:2018-12-04 21:29:38
【问题描述】:

我正在尝试编写一个 powershell 脚本来停止服务。等待这些服务停止,然后对 4 个磁盘运行磁盘碎片整理,然后再次运行服务。 我测试了以下内容,但powershell会一步一步地执行,而我需要powershell等到每个步骤完成才能执行下一步。

Stop-Service -Name 'Service1' -Force
Stop-Serivce -Name 'Service2' -Force
Optmize-Volume -DriveLetter C -Defrag
Optmize-Volume -DriveLetter D -Defrag
Optmize-Volume -DriveLetter E -Defrag
Start-Service -Name 'Service1'
Start-Service -Name 'Serivce2'

提前致谢。

【问题讨论】:

  • 您可以使用任何循环使脚本进入睡眠状态并检查上一个任务是否停止(对于服务将是 (Get-Service "service1").Status -eq "Stopped" 但我手头没有 Win10 来检查如何验证是否 @ 987654323@已完成)。也许运行 Optimize-Volume 作为工作就可以了(然后你只需要检查工作是否已经完成)。

标签: powershell


【解决方案1】:

可以在执行下一条命令前检查服务状态

Stop-Service -Name 'ServiceName' -Force
$serv = Get-service -Name 'ServiceName'
Write-Host $serv.Status
while ($serv.Status -ne "Stopped"){
    Start-Sleep -Seconds 5

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2019-04-25
    相关资源
    最近更新 更多