有四個 Tomcat 服務。我想重新啟動它并將狀態顯示為進度條。停止 4 次,啟動 4 次,總共需要 8 個步驟。我嘗試了以下方法,但不幸的是它沒有給出預期的結果。我也不知道該怎么做。有人可以幫我嗎?
for ($i = 1; (Get-Service -DisplayName *tomcat*).Count -le 5; $i )
{
Write-Progress -Activity "Search in Progress" -Status "$i% Complete:" -PercentComplete $i
Get-Service -DisplayNAme *tomcat* | stop-service -WarningAction SilentlyContinue
}
for ($i = 1; (Get-Service -DisplayName *tomcat*).Count -le 5; $i )
{
Write-Progress -Activity "Search in Progress" -Status "$i% Complete:" -PercentComplete $i
Get-Service -DisplayNAme *tomcat* | start-service -WarningAction SilentlyContinue
}
uj5u.com熱心網友回復:
為什么首先一個回圈停止服務,然后另一個回圈再次啟動它們?
還有一個Restart-Service cmdlet 可以停止然后啟動服務,因此您只需要一個回圈。
此外,如果您Get-Service在變數中捕獲 cmdlet 的結果,則不必一遍又一遍地執行此操作:
# the @() ensures the result is an array so we can use its .Count property
$tomcat = @(Get-Service -DisplayName *tomcat* )
for ($i = 1; $i -le $tomcat.Count; $i ) {
Write-Progress -Activity "Restarting Tomcat service" -Status "$i% Complete:" -PercentComplete $i
$tomcat[$i -1] | Restart-Service -Force -ErrorAction SilentlyContinue
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/462214.html
上一篇:PowershellSort-ObjectPSCustomObject與Object[]作為值
下一篇:如何將陣列傳遞給函式?
