Skip to content

Instantly share code, notes, and snippets.

@mwallner
Last active May 14, 2022 02:53
Show Gist options
  • Save mwallner/367b364639795d504d3980a2687059de to your computer and use it in GitHub Desktop.
Save mwallner/367b364639795d504d3980a2687059de to your computer and use it in GitHub Desktop.
function PrintJobWithData {
param(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
$job
)
if ($job.HasMoreData -eq "True") {
Write-Output "--- BEGIN $($job.Name) BEGIN ---"
try {
$job | Receive-Job
}
finally {
Write-Output "--- END $($job.Name) END ---"
}
}
}
function MonitorJobs($joblist, $interval) {
try {
do {
$jobstate = $joblist | Get-Job | Where-Object -Property State -eq "Running"
$jobstate | Get-Job | ForEach-Object {$_ | PrintJobWithData}
Start-Sleep $interval
} while ($jobstate)
$joblist | Get-Job | ForEach-Object {$_ | PrintJobWithData}
}
catch {
Write-Error ($_.Exception | Format-List -Force | Out-String) -ErrorAction Continue
Write-Error ($_.InvocationInfo | Format-List -Force | Out-String) -ErrorAction Continue
$joblist | Get-Job | Receive-Job | Write-Output
throw
}
finally {
$joblist | Remove-Job -Force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment