Steve Borba

My notes, I hope they help you, feel free to comment/add to them

Loop with Progress Bars and ETA

Here is a script for progress bar and ETA, I grabbed part of it from Jason.

$LoopAverage = 0
$Max = 150
$Speed = 50
$i=0
While ($i -lt $Max) {
  $StartLoopTime = Get-Date
  $ParentLoopText = ("Working on iteration "+$i++)
  if (($i+1 -gt 6) -or ($i+1 -gt ($Max * 0.1))) {
    if ( [Math]::Abs($Delta) -lt ($LoopAverage/3) ) { $Speed = (((50 - ($Delta/$LoopAverage)*150)+$Speed*3)/4) } else { if ( $Delta -lt 0 ) { $Speed = ($Speed + 99)/2 } else { $Speed = $Speed/2 } }
    if ( $Speed -gt 60 ) { $Text = "Speeding up" } Else { if ( $Speed -lt 40 ) { $Text = "Slowing down" } Else { $Text = "Maintaing Speed"}}

    Write-Progress -id 1 -Activity "Running Loop" -Status $ParentLoopText -PercentComplete (($i/$Max)*100) -SecondsRemaining ($LoopAverage*($Max - $i)/1000)
    Write-Progress -ParentId 1 -id 2 -PercentComplete $Speed -Activity ("Average Loop Time: "+[Math]::round($LoopAverage / 100)/10 +" Seconds") -CurrentOperation ($Text)
  } else {
    Write-Progress -id 1 -Activity "Running Loop" -Status $ParentLoopText -PercentComplete (($i/$Max)*100)
    Write-Progress -ParentId 1 -id 2 -PercentComplete $Speed -Activity "Determinng Average"
  }

  #Add Script from here
  sleep -Milliseconds (Get-Random -Maximum (50+$Max-$i) -Minimum ($Max-$i))
  $i2=0
  while ($i2 -lt 4) {
    Write-Progress -ParentId 2 -id 3 -Activity "Innerstep $i2" -PercentComplete ( 40 + ($i2++/4)*60)
    sleep -Milliseconds (get-random -Maximum (50+$Max-$i) -Minimum ($Max-$i))
  }
  #To Here

  $Delta = (New-TimeSpan -Start $StartLoopTime -End (Get-Date)).TotalMilliseconds - $LoopAverage
  if ( (100*($i/$Max)) -lt 10) { $LoopAverage = $LoopAverage + $Delta/$i } Else {
                                 $LoopAverage = $LoopAverage + $Delta/($Max*.1)
     }
}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>