Powershell |将tomcat-Restrat-Process展示为进度栏

发布于 2025-01-23 04:59:21 字数 672 浏览 0 评论 0原文

有四个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
    }

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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞取 2025-01-30 04:59:21

为什么首先要停止服务,然后再循环再次开始呢?
还有一个停止然后开始服务的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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文