# Wednesday, December 05, 2007

Here's another interesting use for my PowerShell Eventing Snap-In, where I'm simulating unix-style foreground/background tasks. In this case, the task is a large download using System.Net.WebClient. Just dot-source the script below and start a download using the Start-Download function, passing the url to the large file and the local path where to save your file (be sure to fully qualify the save path). The download will start immediately and show a progress bar with bytes remaining and a percentage, however you can hit Ctrl+C at any time and the download will continue in the background. You can get back to PowerShell tasks, and bring back up the progress bar by invoking Show-Progress at any time. Use Stop-Download to cancel the currently active download. Only one download can be active at a time, but this could easily be extended to support a pool of downloads (using multiple WebClient objects).

downloads.ps1 (1.85 KB)

  1. #requires -pssnapin pseventing  
  2.  
  3. function Stop-Download {  
  4.     $wc.CancelAsync()  
  5. }  
  6.  
  7. function Start-Download {  
  8.     param(  
  9.         [uri]$Url = $(throw "need Url."),  
  10.         [string]$Path = $(throw "Need save path.")  
  11.     )  
  12.  
  13.     # initialise webclient  
  14.     if (-not $global:wc) {  
  15.         $global:wc = New-Object System.Net.WebClient  
  16.         $var = get-variable wc  
  17.         Connect-EventListener -Variable $var `  
  18.             -EventName DownloadProgressChanged, DownloadFileCompleted         
  19.     }  
  20.  
  21.     if ($wc.IsBusy) {  
  22.         Write-Warning "Currently busy: Please cancel current download or wait until it is completed." 
  23.         return 
  24.     }  
  25.       
  26.     $wc.DownloadFileAsync($url, $path, $path) # last is userstate  
  27.     Write-Host "Download started. Ctrl+C to continue in background." 
  28.     Show-Progress  
  29. }  
  30.  
  31. function Show-Progress {  
  32.     if (-not $wc.IsBusy) {  
  33.         # possibly already completed, clear queue  
  34.         get-event | Out-Null 
  35.         "No active download." 
  36.         return 
  37.     }  
  38.       
  39.     Start-KeyHandler -CaptureCtrlC  
  40.       
  41.     trap [exception] {  
  42.         Stop-KeyHandler  
  43.         Write-Warning "error" 
  44.         $_ 
  45.         return 
  46.     }  
  47.       
  48.     $exiting = $false 
  49.       
  50.       
  51.     while (-not $exiting) {       
  52.         $events = @(Get-Event -Wait)          
  53.         $event = $null;  
  54.           
  55.         # skip to last event  
  56.         foreach ($event in $events) {  
  57.             if ($event.Name -eq "CtrlC") {  
  58.                 break;  
  59.             }  
  60.         }         
  61.           
  62.         switch ($event.Name) {  
  63.             "DownloadProgressChanged" {  
  64.                 $r = $event.args.BytesReceived  
  65.                 $t = $event.args.TotalBytesToReceive  
  66.                 $activity = "Downloading $($event.args.userstate)" 
  67.                 $p = [math]::Ceiling(([double]$r / $t) * 100)  
  68.                 Write-Progress -Activity $activity -PercentComplete $p `  
  69.                     -Status ("{0} of {1} byte(s)" -f $r,$t)  
  70.             }  
  71.             "DownloadFileCompleted" {  
  72.                 Write-Progress -Activity DownloadComplete -Completed  
  73.                 "Complete." 
  74.                 $exiting = $true 
  75.             }  
  76.             "CtrlC" {  
  77.                 "Switching to background downloading. Show-Progress to move to foreground." 
  78.                 $exiting = $true 
  79.             }  
  80.         }         
  81.     }  
  82.     Stop-KeyHandler  
posted on Wednesday, December 05, 2007 7:47:42 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] Trackback
Related posts:
PowerShell v2.0 – Differences Between CTP3/Win7Beta and Win7RC
Pscx 1.2 Beta Released
PowerShell 2.0 CTP3: Modules, in Practice - Closures
PowerShell 2.0 CTP3: Modules, in Practice – .NET Interfaces
PowerShell 2.0 CTP3: Modules, in Practice.
Visual Studio 2008 Extensions for WSS 3.0 v1.3 March CTP

Referred by:
http://www.codeplex.com/PSEventing [Referral]
+primalforms +powershell +"progress bar" (www.google.com) [Referral]
download (search.live.com) [Referral]
http://www.codeplex.com/PSEventing/Release/ProjectReleases.a... [Referral]
powershell getvariable snapin (www.google.com) [Referral]
webclient TotalBytesToReceive (search.yahoo.com) [Referral]
primalforms progress bar (www.google.com) [Referral]
powershell +"progress bar" System.Windows.Forms (www.google.co.uk) [Referral]
powershell math percent (www.google.com) [Referral]
powershell get-variable -scope (www.google.com) [Referral]
powershell write-progress system.net.webclient (www.google.com) [Referral]
http://www.vistax64.com/powershell/159205-pseventing-1-1-pow... [Referral]
powershell url download (www.google.se) [Referral]
Powershell HTTP webclient (search.live.com) [Referral]
http://www.google.com/ [Referral]
powershell write-progress webclient (www.google.com) [Referral]
powershell Stop-KeyHandler (www.google.co.kr) [Referral]
powershell show progress (www.google.com) [Referral]
powershell forms ctrl^C to continue (www.google.be) [Referral]
powershell show multiple progress bars (www.google.com) [Referral]
how to stop downloading webclient DownloadFileAsync (www.google.com) [Referral]
progress bar powershell style (www.google.ch) [Referral]
bring object foreground powershell (www.google.co.uk) [Referral]
RunspaceFactory foreground (www.google.nl) [Referral]
http://www.vistax64.com/powershell/114621-execute-clean-up-c... [Referral]
DownloadProgressChanged in asp.net (www.google.co.in) [Referral]
foreground powershell (www.google.ch) [Referral]
PowerShell Show Progress (www.google.com) [Referral]
primalforms "progress bar" (www.google.com) [Referral]
progressBar +powershell (www.google.ch) [Referral]
progressBar +powershell (www.google.ch) [Referral]
PowerShell form event args (www.google.com.by) [Referral]
powershell DownloadProgressChanged (www.google.es) [Referral]
http://pseventing.codeplex.com/ [Referral]
DownloadFileAsync (www.kumo.com) [Referral]
PowerShell Eventing Snap-In download (www.google.com) [Referral]
http://pseventing.codeplex.com/Release/ProjectReleases.aspx?... [Referral]
powershell display progress (www.google.co.uk) [Referral]
http://www.techtalkz.com/microsoft-windows-powershell/417195... [Referral]
powershell webclient (search.live.com) [Referral]
DownloadProgressChanged asp.net (www.google.com.eg) [Referral]
powershell window forms busy bar (www.google.nl) [Referral]
foreground background trap (www.google.de) [Referral]
powershell System.Net.WebClient status (www.kumo.com) [Referral]
DownloadFileCompleted+large file (www.google.co.ma) [Referral]
powershell busy bar (www.google.nl) [Referral]
powershell progress status database (www.google.com) [Referral]
powershell foreach foreground (www.google.ca) [Referral]
PrimalForms how to Progress bar (www.google.com) [Referral]
Powershell Write-progress install (www.google.com) [Referral]
webclient DownloadFileAsync how to stop (www.google.pl) [Referral]
http://www.dogpile.com/dogpile/ws/results/Web/dotnet%20how%2... [Referral]
unix background task foreground (www.google.co.th) [Referral]
http://fiskekommunerna.org/discount/commercial-carpet-cleani... [Referral]
http://www.codeplex.com/PSEventing/ [Referral]
powershell + foreground (www.google.fr) [Referral]
asp.net webclient DownloadFileCompleted progress (www.google.com.hk) [Referral]
powershell progress bar (www.google.com.ar) [Referral]
Windows.Forms.ProgressBar powershell (www.google.com) [Referral]
progressbar powershell windows forms (www.google.com) [Referral]
powershell global variable (search.live.com) [Referral]
progressbar+powershell (www.google.ca) [Referral]
PercentComplete EventHandler powershell (www.google.de) [Referral]
www.kumo.com (www.ask.com) [Referral]
www.kumo.com (www.ask.com) [Referral]
powershell write-busy (www.google.co.uk) [Referral]
DownloadProgressChanged powershell (www.google.com) [Referral]
powershell write-progress eventing (www.google.de) [Referral]
background task powershell (www.google.co.in) [Referral]
powershell start background task (www.google.com) [Referral]
powershell System.Net.WebClient (www.bing.com) [Referral]
http://www.netvibes.com/epupz [Referral]
powershell bring window to foreground (www.google.de) [Referral]
powershell show progress (www.google.co.uk) [Referral]
DownloadFileAsync,stop-download (www.google.de) [Referral]
cancelasync causes null exception while downloadfileasync (www.google.com) [Referral]
powershell write-progress .net event (www.google.de) [Referral]
progress barr powershell (www.google.it) [Referral]
background downloading function (www.google.is) [Referral]
powershell background foreground (www.google.ch) [Referral]
http://www.netvibes.com/abercrombieandfitchclothing [Referral]
powershell forms progressbar (www.google.nl) [Referral]
webclient downloadfileasync "webclient objects" (www.google.co.uk) [Referral]
background downloads (www.google.com) [Referral]
add _ DownloadFileCompleted powershell (www.google.cz) [Referral]
webclient downloadfileasync (www.google.com) [Referral]
bring application to foreground powershell (www.google.com) [Referral]
powershell forms progressbar (www.google.se) [Referral]
powershell stop progress bar (www.google.co.uk) [Referral]
Comments are closed.