Skip to content

Instantly share code, notes, and snippets.

Created May 1, 2014 21:17
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 anonymous/3ad9e4e741c2fd274c86 to your computer and use it in GitHub Desktop.
Save anonymous/3ad9e4e741c2fd274c86 to your computer and use it in GitHub Desktop.
$scriptblock = {
$repetitions = 10
for($i = 1; $i -le $repetitions; $i++)
{
#The work is currently simulated by sleeping
Start-Sleep -Seconds 2
#Write some information about how far we have come
Write-Output @{
Message = "We're at step $i"
PercentComplete = ($i * 100 / $repetitions)
}
}
}
$job = Start-Job $scriptblock
#This implementation does not work if the job goes to "Failed" state or other error state
while($job.State -ne "Completed" -or $job.HasMoreData)
{
#This implementation does not sum all work, instead takes only the last output information
$output = $job | Receive-Job | Select -Last 1
if ($output)
{
$progressInfo = @{
Activity = "Work"
Status = "Working"
PercentComplete = $output.PercentComplete
CurrentOperation = $output.Message
}
Write-Progress @progressInfo
}
Start-Sleep -Seconds 1
}
Write-Progress -Activity "Work" -Completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment