简体   繁体   English

Powershell停止服务,清理磁盘,然后启动服务

[英]Powershell Stop Services, defrg disks and then start services

Im trying to write a powershell script to stop services. 我试图编写一个Powershell脚本来停止服务。 WAIT for these services to stop and then run disk defrag for 4 disks and then run services again. 等待这些服务停止,然后为4个磁盘运行磁盘碎片整理,然后再次运行服务。 I tested the below but powershell keep performing step after step while i need powershell to wait until each step is finished to perform the next step. 我测试了以下内容,但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'

Thanks in advance. 提前致谢。

You can check the status of the service before executing the next command 您可以在执行下一个命令之前检查服务的状态

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM