gpt4 book ai didi

arrays - 编写进度条,直到Get-Job返回 “Running” PowerShell

转载 作者:行者123 更新时间:2023-12-03 00:39:37 25 4
gpt4 key购买 nike

我有一组正在运行的作业。

PS C:\vinith> Get-Job

Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
2 scvmm2012-vin BackgroundJob Running True localhost Param...
4 scom2012sp1-vin BackgroundJob Running True localhost Param...
6 scorch2012-vin BackgroundJob Running True localhost Param...
8 scsm2012sp1-vin BackgroundJob Running True localhost Param...
10 spfoundation BackgroundJob Running True localhost Param...

我希望进度条显示到作业运行为止,并且在Powershell中作业状态变为“完成”时应该说完成

最佳答案

使用Write-Progress作为进度条。使用Get-Job接收当前作业数。像这样

# Some dummy jobs for illustration purposes
start-job -ScriptBlock { start-sleep -Seconds 5 }
start-job -ScriptBlock { start-sleep -Seconds 10 }
start-job -ScriptBlock { start-sleep -Seconds 15 }
start-job -ScriptBlock { start-sleep -Seconds 20 }
start-job -ScriptBlock { start-sleep -Seconds 25 }

# Get all the running jobs
$jobs = get-job | ? { $_.state -eq "running" }
$total = $jobs.count
$runningjobs = $jobs.count

# Loop while there are running jobs
while($runningjobs -gt 0) {
# Update progress based on how many jobs are done yet.
write-progress -activity "Events" -status "Progress:" `
-percentcomplete (($total-$runningjobs)/$total*100)

# After updating the progress bar, get current job count
$runningjobs = (get-job | ? { $_.state -eq "running" }).Count
}

关于arrays - 编写进度条,直到Get-Job返回 “Running” PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com