1

i have a set of job's which are running.

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...

I want a progress bar display till the jobs are running and should say completed when the job state becomes "completed" in powershell

PowerShell
  • 1,991
  • 8
  • 35
  • 57
  • [Here's a similar question](http://stackoverflow.com/questions/4317438/monitoring-jobs-in-a-powershell-session-from-another-powershell-session) – E.V.I.L. Apr 01 '13 at 23:01

2 Answers2

5

Use Write-Progress for the progress bar. Use Get-Job to receive number of current jobs. Like so,

# 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
}
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Hello Von, i tried it but i do not see any status for -percentcomplete?, its just showing up as blank, here's the job which i started, Start-Job -ScriptBlock { Get-Process| ConvertTo-Json}, and ran your script above to show progress but i did not see any status for percent complete --> it did not advance with small "o" dots, it was just blank – PowerShell Apr 01 '13 at 10:03
  • Worked for me. Of course you'll only see progress once jobs are not in `Running` state anymore. – Ansgar Wiechers Apr 01 '13 at 11:12
1

Use the following in your while block to have a value in the progress bar as well

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
CrazySpy
  • 153
  • 1
  • 1
  • 14