Advertisement
mariussm

New-Progressbar

Jun 7th, 2014
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Synopsis
  3.    Wrapper module for progressbar, making it even easier to implement in scripts
  4. .DESCRIPTION
  5.    Wrapper module for progressbar, making it even easier to implement in scripts
  6. .EXAMPLE
  7.    $bar = New-Progressbar -TotalCount 10
  8.    Creates a new progressbar object, on which you can operate easily:
  9.    1..10 | foreach{$bar.Progress(); sleep 1; }
  10.    $bar.Complete()
  11. .EXAMPLE
  12.    $bar = New-Progressbar -TotalCount 10 -ActivityName "Processing files"
  13.    Use custom activity name
  14. .EXAMPLE
  15.    1..10 | foreach{$bar.Progress("Example - {0}" -f $_); sleep 1; }
  16.    Use custom status message
  17. #>
  18. function New-Progressbar
  19. {
  20.     [CmdletBinding()]
  21.     Param
  22.     (
  23.         # Total count
  24.         [Parameter(Mandatory=$true,
  25.                    ValueFromPipelineByPropertyName=$false,
  26.                    Position=0)]
  27.         [int]$TotalCount,
  28.  
  29.         # Activity name
  30.         [Parameter(Mandatory=$false,
  31.                    ValueFromPipelineByPropertyName=$false,
  32.                    Position=1)]
  33.         [string]$ActivityName = "Running",
  34.  
  35.         # Time estimation
  36.         [Parameter(Mandatory=$false,
  37.                    ValueFromPipelineByPropertyName=$false,
  38.                    Position=2)]
  39.         [boolean]$TimeEstimationEnabled = $true
  40.     )
  41.  
  42.     # Create new module instance  
  43.     $m =  New-Module -ScriptBlock {
  44.         # Internal variables
  45.         $script:total = 1;
  46.         $script:current = 0;
  47.         $script:ActivityName = " ";
  48.         $script:startTime = Get-Date;
  49.         $script:timeEstimation = $false;
  50.         # Functions with obvious method names
  51.         function setActivityName($name) {$script:ActivityName = $name}
  52.         function setTotal($tot) { $script:total = $tot}
  53.         function getTotal($tot) { return $script:total}
  54.         function enableTimeEstimation() {$script:timeEstimation = $true}
  55.         function disableTimeEstimation() {$script:timeEstimation = $false}
  56.  
  57.  
  58.         # Progress the progressbar one step. Optional parameter Text for defining the status message
  59.         function Progress {
  60.             Param
  61.             (
  62.                 [Parameter(Mandatory=$false,
  63.                     ValueFromPipelineByPropertyName=$false,
  64.                     Position=0)]
  65.                 [string]$Text = ("{0}/{1}" -f $script:current, $script:total)
  66.             )
  67.  
  68.             $params = @{
  69.                 Activity = $script:ActivityName
  70.                 Status = $Text
  71.                 PercentComplete = ($script:current / $script:total * 100)
  72.             }
  73.  
  74.             if($script:timeEstimation) {
  75.                 if($script:current -gt 5) {
  76.                     $params["SecondsRemaining"] = (((Get-Date) - $script:startTime).TotalSeconds / $script:current) * ($script:total - $script:current)
  77.                 }
  78.             }
  79.  
  80.             Write-Progress @params
  81.            
  82.             if($script:current -lt $script:total) {
  83.                 $script:current += 1
  84.             } else {
  85.                 Write-Warning "Progressbar incremented too far"
  86.             }
  87.         }
  88.         function Complete() {Write-Progress -Activity $script:ActivityName -Status $script:total -PercentComplete 100 -Completed}
  89.         export-modulemember -function setTotal,getTotal,Progress,Complete,setActivityName,enableTimeEstimation,disableTimeEstimation
  90.     } -AsCustomObject
  91.  
  92.     # Set initial values
  93.     $m.setTotal($TotalCount)
  94.     $m.setActivityName($ActivityName)
  95.  
  96.     if($TimeEstimationEnabled) {
  97.         $m.enableTimeEstimation()
  98.     }
  99.  
  100.     return $m;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement