Last active
November 1, 2025 08:22
-
-
Save fdcastel/4f1ce07951c433c9e3ed1c8178bec40c to your computer and use it in GitHub Desktop.
Powershell demo showing how to report the status from running jobs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Original code by Mike Smith -- https://stackoverflow.com/a/67510672/33244 | |
| # Load your jobs here... Note that they periodically report their status. | |
| Start-Job -Name "LoopA" -ScriptBlock { $i = 0; 1..5 | ForEach-Object { Start-Sleep -Seconds 1; $i++; "$i of 5 complete" }; Start-Sleep -Seconds 1; } | |
| Start-Job -Name "LoopB" -ScriptBlock { $i = 0; 1..5 | ForEach-Object { Start-Sleep -Seconds 2; $i++; "$i of 5 complete" }; Start-Sleep -Seconds 1; } | |
| Start-Job -Name "LoopC" -ScriptBlock { $i = 0; 1..5 | ForEach-Object { Start-Sleep -Seconds 3; $i++; "$i of 5 complete" }; Start-Sleep -Seconds 1; } | |
| # Loop and report status | |
| do { | |
| # Interval between updates; change for your application | |
| Start-Sleep -Milliseconds 500 | |
| # Snapshot of current jobs | |
| $jobs = Get-Job | |
| # Report status | |
| Clear-Host | |
| $jobs ` | |
| | Select-Object Name, State, @{Name = "LastMsg"; Expression = { $_ | Receive-Job -Keep | Select-Object -Last 1 } } ` | |
| | Format-Table | |
| } until (-not ($jobs | Where-Object State -eq "Running")) | |
| # Clean up. | |
| Get-Job | Remove-Job -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment