1

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

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 中变为“完成”时应该说已完成

4

2 回答 2

5

用于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
}
于 2013-04-01T08:45:45.187 回答
1

在您的 while 块中使用以下内容在进度条中也有一个值

while($runningjobs -gt 0) {
# Update progress based on how many jobs are done yet.
$percent=[math]::Round((($total-$runningjobs)/$total * 100),2)
write-progress -activity "Starting Provisioning Modules Instances" -status "Progress: $percent%" -percentcomplete (($total-$runningjobs)/$total*100)

# After updating the progress bar, get current job count
$runningjobs = (get-job | ? { $_.state -eq "running" }).Count
于 2016-08-31T13:45:28.283 回答