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

标签 arrays powershell

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

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/

相关文章:

C++ 如何创建一个动态 vector 数组?

c++ - 读取和写入缓冲区

PowerShell 错误 “execution of scripts is disabled on this system.”

xml - 如何使用 Powershell 在 xml 中附加新属性?

powershell - 如何使用 Powershell 中的 Edge Selenium webdriver 检查窗口是否最小化

javascript - Promise.map() 进度指示器

javascript - 使用 php 创建测验,将数组数据存储在数据库的字段中

JavaScript:重新排序数组

regex - 使用 Powershell 替换正则表达式结果的小节

git - 尝试使用 Powershell 从 Azure DevOps Git 存储库下载文件