# 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 Script Provider
PowerShell ISE Hacking: Change default save encoding to ASCII
PowerShell 2.0 – PSCX Labs: Invoke-Reflector
PowerShell 2.0 – Developer Essentials #1 – Initializing a Runspace with a Module
SharePoint Resources & Localization – What, Where and Why?
PowerShell 2.0 – Partial Application of Functions and Cmdlets

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]
DownloadFileCompleted userstate (www.google.es) [Referral]
WebClient DownloadFileAsync DownloadFileCompleted (www.google.co.kr) [Referral]
powershell background task (www.google.co.uk) [Referral]
webclient.downloadfilecompleted (www.google.com) [Referral]
http://s2-us2.ixquick.com/do/metasearch.pl?cmd=process_searc... [Referral]
downloadfileasync exception (www.google.com) [Referral]
powershell background tasks (www.google.com) [Referral]
WebClient DownloadFileAsync DownloadFileCompleted (www.google.com) [Referral]
multiple webclient.downloadfileasync (www.google.com.ar) [Referral]
DownloadFileCompleted (www.google.com.br) [Referral]
multiple webclient.downloadfileasync with progressbar (www.google.com.ar) [Referral]
primalforms progress bar (www.google.com) [Referral]
powershell bring application to foreground (www.google.com) [Referral]
powershell foreground (www.google.de) [Referral]
math in powershell to show as percentage (www.google.com) [Referral]
download (www.bing.com) [Referral]
webclient downloadfileasync restart (www.google.com) [Referral]
powershell webclient (www.bing.com) [Referral]
powershell form progress bar (www.bing.com) [Referral]
.net DownloadFileCompleted wait (www.google.com) [Referral]
WebClient.Download in SharePoint + PowerShell (www.google.co.in) [Referral]
powershell write-progress execute (www.bing.com) [Referral]
http://www.worio.com/ [Referral]
background tasks in powershell (www.bing.com) [Referral]
powershell CTRL+C Stopping (www.bing.com) [Referral]
powershell downloadfileasync (www.bing.com) [Referral]
powershell form progressbar (www.bing.com) [Referral]
powershell script bring application from background to foreground (www.google.com) [Referral]
DownloadFileCompleted userstate (www.google.ru) [Referral]
Powershell foreground (www.google.nl) [Referral]
TotalBytesToReceive (www.google.com) [Referral]
powershell bring window to foreground (www.google.com) [Referral]
powershell DownloadFileCompleted (www.google.de) [Referral]
"powershell form progressbar" (www.google.ch) [Referral]
cache:wSzRcLtqL7cJ:www.codeplex.com/PSEventing powershell handling events (209.85.229.132) [Referral]
write-progress primalforms (www.google.co.uk) [Referral]
DownloadFileAsync stop (www.google.co.uk) [Referral]
fortschrittsanzeiger von windows in powershell nutzen (www.google.de) [Referral]
primalforms pogress bar (www.google.com.au) [Referral]
powershell background task (www.bing.com) [Referral]
"multiple webclient" powershell (www.google.com) [Referral]
CompactFramework progressbar Background (search.yahoo.co.jp) [Referral]
powershell primalforms progressbar (www.google.com) [Referral]
powershell downloadfileasync (www.google.com) [Referral]
windows form progress bar + powershell (www.google.co.in) [Referral]
powershell background task (www.google.com.au) [Referral]
powershell background tasks (www.bing.com) [Referral]
multiple DownloadFileAsync simultaneous (www.google.com.tw) [Referral]
downloadfileasync error handling (www.bing.com) [Referral]
primalforms progressbar (www.google.co.uk) [Referral]
powershell forms progress (www.google.ch) [Referral]
"primalforms" foreground (www.google.de) [Referral]
powershell web client (www.bing.com) [Referral]
powershell progressbar webclient (www.google.ch) [Referral]
powershell bring app to foreground (www.google.com) [Referral]
multiple url powershell (www.bing.com) [Referral]
DownloadFileAsync webClient yahoo.co.jp (search.yahoo.co.jp) [Referral]
primalform progress bar (www.google.com) [Referral]
foreground in powershell (www.google.com) [Referral]
http://translate.googleusercontent.com/translate_c?hl=de&sl=... [Referral]
DownloadProgressChanged powershell (www.google.com) [Referral]
powershell application foreground (www.google.co.uk) [Referral]
http://subtextproject.com/Services/default.htm [Referral]
http://pseventing.codeplex.com/releases/view/5919 [Referral]
download big files powershell (www.google.com) [Referral]
http://www.netvibes.com/cheapautoinsurancequote [Referral]
System.Windows.Forms.ProgressBar powershell (www.google.se) [Referral]
DownloadFileAsync cancel (www.google.co.uk) [Referral]
downloadfilecompleted (search.conduit.com) [Referral]
powershell background task (www.bing.com) [Referral]
powershell httpwebclient (www.google.dk) [Referral]
powershell DownloadFileCompleted (www.google.ca) [Referral]
powershell webclient downloadfileasync (www.bing.com) [Referral]
DownloadFileAsync asp (www.google.com) [Referral]
powershell get bing wallpaper (www.google.fr) [Referral]
powershell bring window in foreground (www.google.de) [Referral]
powershell DownloadProgressChanged (www.google.com) [Referral]
add DownloadProgressChanged powershell (www.google.de) [Referral]
powershell bing wallpaper (www.bing.com) [Referral]
WebClient DownloadFileCompleted Event (www.bing.com) [Referral]
DownloadProgressChanged powershell (www.bing.com) [Referral]
powershell DownloadFileCompleted (www.bing.com) [Referral]
powershell system.webclient DownloadProgresschanged progress bar (www.google.co.in) [Referral]
powershell downloadfilecompleted (www.bing.com) [Referral]
powershell webclient downloadfileasync (cn.bing.com) [Referral]
primalforms progressbar (www.google.nl) [Referral]
http://pseventing.codeplex.com/Wikipage [Referral]
Powershell show progress (www.google.ru) [Referral]
get percentage powershell (www.bing.com) [Referral]
primalforms progressbar (www.google.fr) [Referral]
powershell background (www.bing.com) [Referral]
primalforms progressbar (www.google.nl) [Referral]
http://pseventing.codeplex.com/wikipage [Referral]
asp.net powershell download (www.bing.com) [Referral]
powershell progress indicator not exiting (www.google.co.in) [Referral]
WebClient..::.DownloadFileAsync (www.bing.com) [Referral]
powershell trap ctrl-c (www.bing.com) [Referral]
ProgressBar DownloadFileAsync (tw.search.yahoo.com) [Referral]
primalforms progressbar (www.google.be) [Referral]
progress bar primalforms (www.google.com) [Referral]
"download in sharepoint" (www.google.com.my) [Referral]
webclient progress indication powershell (www.bing.com) [Referral]
DownloadFileCompleted powershell (www.google.com) [Referral]
powershell webclient (www.bing.com) [Referral]
http://www.baidu.com/s?bs=edengundam+powershell+webclient&f=... [Referral]
powershell script function in background (www.google.com) [Referral]
downloadfilecompleted +cancel async (www.google.com.au) [Referral]
powershell DownloadProgressChanged (www.google.de) [Referral]
downloadfilecompleted userstate (www.google.com) [Referral]
downloadprogresschanged powershell (www.google.com) [Referral]
http://www.google.se/ [Referral]
System.Windows.Forms.BusyBar Powershell (www.google.ch) [Referral]
System.Windows.Forms.BusyBar Powershell examples (www.google.ch) [Referral]
powershell Set Foreground Window (www.google.com) [Referral]
powershell webclient download file write progress (www.bing.com) [Referral]
http://www.google.co.in/ [Referral]
bing-wallpaper (www.bing.com) [Referral]
Set Foreground application powershell (www.google.gr) [Referral]
http://www.netvibes.com/zetia [Referral]
WebClient.DownloadProgressChanged powershell (www.google.com) [Referral]
cache:67e2DJMU2jwJ:www.nivot.org/2007/12/05/ForegroundBackgroundSwappableDownloadsInPowerShell.aspx WebClient.DownloadProgressChanged powershell (webcache.googleusercontent.com) [Referral]
WebClient DownloadFileCompleted Event (www.bing.com) [Referral]
powershell foreground (www.bing.com) [Referral]
Comments are closed.