Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ForEach-Parallel with a progress bar

By: gigglestick on May 13th, 2013  |  syntax: Power Shell  |  size: 2.25 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. [cmdletbinding()]
  2. param (
  3.         [Parameter(Mandatory=$true,position=0)]
  4.         [System.Management.Automation.ScriptBlock] $ScriptBlock,
  5.         [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
  6.         [PSObject]$InputObject,
  7.         [Parameter(Mandatory=$false)]
  8.         [int]$MaxThreads=10,
  9.         [switch]$Progress,
  10.         [string]$Activity=$MyInvocation.MyCommand.Path
  11. ) # param
  12.  
  13. begin {
  14.         $iss         = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
  15.         $pool        = [Runspacefactory]::CreateRunspacePool(1, $MaxThreads, $iss, $host)
  16.         $threads     = @()
  17.         $ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock( "param(`$this)`r`n" + $Scriptblock.ToString() )
  18.         $pool.open()
  19.  
  20.         $completedthreads = 0
  21.         $totalthreads     = 0
  22. } # begin
  23.  
  24. process {
  25.     $powershell = [powershell]::Create().addscript($scriptblock).addargument($InputObject)
  26.     $powershell.runspacepool = $pool
  27.     $threads += @{
  28.                 instance = $powershell
  29.                 handle = $powershell.begininvoke()
  30.     }
  31.        
  32.         $totalthreads +=1
  33.    
  34.         if ( $Progress ) {
  35.                 for ( $i=0; $i -lt $threads.count; $i++ ) {
  36.                         $thread = $threads[$i]
  37.                         if ( $thread ) {
  38.                                 if ( $thread.handle.iscompleted ) {
  39.                                         $thread.instance.endinvoke( $thread.handle )
  40.                                         $thread.instance.dispose()
  41.                                         $threads[$i] = $null
  42.                                         $completedthreads += 1
  43.                                 }
  44.                         } # if ( $thread )
  45.                 } # for
  46.                 Write-Progress -Activity $Activity -Status "$completedthreads of $totalthreads completed" -PercentComplete (100*$completedthreads/$totalthreads)
  47.         }
  48. } # process
  49.  
  50. end {
  51.         $notdone = $true
  52.         while ( $notdone ) {
  53.                 $notdone = $false
  54.                 for ( $i=0; $i -lt $threads.count; $i++ ) {
  55.                         $thread = $threads[$i]
  56.                         if ( $thread ) {
  57.                                 if ( $thread.handle.iscompleted ) {
  58.                                         $thread.instance.endinvoke( $thread.handle )
  59.                                         $thread.instance.dispose()
  60.                                         $threads[$i] = $null
  61.                                         $completedthreads += 1
  62.                                 } # if ( $thread.handle.iscompleted )
  63.                 else {
  64.                     $notdone = $true
  65.                 }
  66.                         }
  67.                 } # for $i
  68.                 if ( $Progress ) { Write-Progress -Activity $Activity -Status "$completedthreads of $totalthreads completed" -PercentComplete (100*$completedthreads/$totalthreads) }
  69.         } # while ($notdone)
  70.         if ( $Progress ) { Start-Sleep -Seconds 1; Write-Progress -Activity $Activity -Status " " -Completed }
  71. } # end
clone this paste RAW Paste Data