There are four Tomcat services. I would like to restart it and display the status as a progress bar. Stopping 4 times and starting 4 times would be a total of 8 steps. I've tried the following, but unfortunately it doesn't give the expected result. I have no idea how to do it either. Could anyone help me, please?
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
}
CodePudding user response:
Why first a loop to stop the services and then another loop to start them up again?
There is also a Restart-Service cmdlet that stops and then starts a service, so with that you only need one loop.
Also, if you capture the result of the Get-Service
cmdlet in a variable, you won't have to do that over and over again:
# 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
}