Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Last active February 8, 2017 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heiswayi/5742f47362ebbb2d5cc35f6a1eb4fc36 to your computer and use it in GitHub Desktop.
Save heiswayi/5742f47362ebbb2d5cc35f6a1eb4fc36 to your computer and use it in GitHub Desktop.
Update progress in PowerShell
$Num = 5
$Jobs = @()
ForEach ($Job in (1..$Num))
{ $Jobs += Start-Job -ScriptBlock {
$Count = 1
Do {
Write-Progress -Id 2 -Activity "Background Job" -Status $Count -PercentComplete 1
$Count ++
Start-Sleep -Seconds 4
} Until ($Count -gt 5)
}
}
$Total = $Num * 5
Write-Progress -Id 1 -Activity "Watching Background Jobs" -Status "Waiting for background jobs to started" -PercentComplete 0
Do {
$TotProg = 0
ForEach ($Job in $Jobs)
{ Try {
$Info =
$TotProg += ($Job | Get-Job).ChildJobs[0].Progress.StatusDescription[-1]
}
Catch {
Start-Sleep -Seconds 3
Continue
}
}
If ($TotProg)
{ Write-Progress -Id 1 -Activity "Watching Background Jobs" -Status "Waiting for background jobs to complete: $TotProg of $Total" -PercentComplete (($TotProg / $Total) * 100)
}
Start-Sleep -Seconds 3
} Until (($Jobs | Where State -eq "Running").Count -eq 0)
$Jobs | Remove-Job -Force
Write-Progress -Id 1 -Activity "Watching Background Jobs" -Status "Completed! $Total of $Total" -PercentComplete 100
Start-Sleep -Seconds 1
Write-Progress -Id 1 -Activity "Watching Background Jobs" -Status "Completed! $Total of $Total" -Completed
for ($a=1; $a -lt 100; $a++) {
Write-Progress -Activity "Working..." -Status "Please wait" -PercentComplete $a -CurrentOperation "$a% complete"
Start-Sleep 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment