From 11e566ee1f252ca94af44dee9d4a31f1b49d1e42 Mon Sep 17 00:00:00 2001 From: Stephen Austin Date: Thu, 9 Aug 2012 04:43:04 -0400 Subject: [PATCH 1/3] Added support for files located on FTP servers. Added new Get-FtpFile.ps1 script modeled after the existing Get-WebFile.ps1. --- .../functions/Get-ChocolateyWebFile.ps1 | 140 +++++++++--------- src/helpers/functions/Get-FtpFile.ps1 | 58 ++++++++ 2 files changed, 129 insertions(+), 69 deletions(-) create mode 100644 src/helpers/functions/Get-FtpFile.ps1 diff --git a/src/helpers/functions/Get-ChocolateyWebFile.ps1 b/src/helpers/functions/Get-ChocolateyWebFile.ps1 index 91b4246..7e39e91 100644 --- a/src/helpers/functions/Get-ChocolateyWebFile.ps1 +++ b/src/helpers/functions/Get-ChocolateyWebFile.ps1 @@ -1,70 +1,72 @@ -function Get-ChocolateyWebFile { -<# -.SYNOPSIS -Downloads a file from the internets. - -.DESCRIPTION -This will download a file from a url, tracking with a progress bar. -It returns the filepath to the downloaded file when it is complete. - -.PARAMETER PackageName -The name of the package we want to download - this is arbitrary, call it whatever you want. -It's recommended you call it the same as your nuget package id. - -.PARAMETER FileFullPath -This is the full path of the resulting file name. - -.PARAMETER Url -This is the url to download the file from. - -.PARAMETER Url64bit -OPTIONAL - If there is an x64 installer to download, please include it here. If not, delete this parameter - -.EXAMPLE -Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT' - -.NOTES -This helper reduces the number of lines one would have to write to download a file to 1 line. -There is no error handling built into this method. - -.LINK -Install-ChocolateyPackage -#> -param( - [string] $packageName, - [string] $fileFullPath, - [string] $url, - [string] $url64bit = $url -) - Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`',and url64bit:`'$url64bit`'"; - - $url32bit = $url; - $processor = Get-WmiObject Win32_Processor - $procCount=(Get-WmiObject Win32_ComputerSystem).NumberofProcessors - if ($procCount -eq '1') { - $is64bit = $processor.AddressWidth -eq 64 - Write-Debug "Processor width is $($processor.AddressWidth)." - } else { - Write-Debug "First processor width is $($processor[0].AddressWidth)." - $is64bit = $processor[0].AddressWidth -eq 64 - } - $systemBit = '32 bit' - if ($is64bit) { - $systemBit = '64 bit'; - $url = $url64bit; - } - - $downloadMessage = "Downloading $packageName ($url) to $fileFullPath" - if ($url32bit -ne $url64bit) {$downloadMessage = "Downloading $packageName $systemBit ($url) to $fileFullPath.";} - Write-Host "$downloadMessage" - #$downloader = new-object System.Net.WebClient - #$downloader.DownloadFile($url, $fileFullPath) - if ($url.StartsWith('http')) { - Get-WebFile $url $fileFullPath - } else { - Write-Debug "We are attempting to copy the local item `'$url`' to `'$fileFullPath`'" - Copy-Item $url -Destination $fileFullPath -Force - } - - Start-Sleep 2 #give it a sec or two to finish up +function Get-ChocolateyWebFile { +<# +.SYNOPSIS +Downloads a file from the internets. + +.DESCRIPTION +This will download a file from a url, tracking with a progress bar. +It returns the filepath to the downloaded file when it is complete. + +.PARAMETER PackageName +The name of the package we want to download - this is arbitrary, call it whatever you want. +It's recommended you call it the same as your nuget package id. + +.PARAMETER FileFullPath +This is the full path of the resulting file name. + +.PARAMETER Url +This is the url to download the file from. + +.PARAMETER Url64bit +OPTIONAL - If there is an x64 installer to download, please include it here. If not, delete this parameter + +.EXAMPLE +Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT' + +.NOTES +This helper reduces the number of lines one would have to write to download a file to 1 line. +There is no error handling built into this method. + +.LINK +Install-ChocolateyPackage +#> +param( + [string] $packageName, + [string] $fileFullPath, + [string] $url, + [string] $url64bit = $url +) + Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`',and url64bit:`'$url64bit`'"; + + $url32bit = $url; + $processor = Get-WmiObject Win32_Processor + $procCount=(Get-WmiObject Win32_ComputerSystem).NumberofProcessors + if ($procCount -eq '1') { + $is64bit = $processor.AddressWidth -eq 64 + Write-Debug "Processor width is $($processor.AddressWidth)." + } else { + Write-Debug "First processor width is $($processor[0].AddressWidth)." + $is64bit = $processor[0].AddressWidth -eq 64 + } + $systemBit = '32 bit' + if ($is64bit) { + $systemBit = '64 bit'; + $url = $url64bit; + } + + $downloadMessage = "Downloading $packageName ($url) to $fileFullPath" + if ($url32bit -ne $url64bit) {$downloadMessage = "Downloading $packageName $systemBit ($url) to $fileFullPath.";} + Write-Host "$downloadMessage" + #$downloader = new-object System.Net.WebClient + #$downloader.DownloadFile($url, $fileFullPath) + if ($url.StartsWith('http')) { + Get-WebFile $url $fileFullPath + } elseif ($url.StartsWith('ftp')) { + Get-FtpFile $url $fileFullPath + } else { + Write-Debug "We are attempting to copy the local item `'$url`' to `'$fileFullPath`'" + Copy-Item $url -Destination $fileFullPath -Force + } + + Start-Sleep 2 #give it a sec or two to finish up } \ No newline at end of file diff --git a/src/helpers/functions/Get-FtpFile.ps1 b/src/helpers/functions/Get-FtpFile.ps1 new file mode 100644 index 0000000..88743e6 --- /dev/null +++ b/src/helpers/functions/Get-FtpFile.ps1 @@ -0,0 +1,58 @@ +## Get-FtpFile +############################################################################################################## +## Downloads a file from ftp +## Some code from http://stackoverflow.com/questions/265339/whats-the-best-way-to-automate-secure-ftp-in-powershell +## Additional functionality emulated from http://poshcode.org/417 (Get-WebFile) +## Written by Stephen C. Austin, Pwnt & Co. http://pwnt.co +############################################################################################################## +function Get-FtpFile { +param( + $url = '', #(Read-Host "The URL to download"), + $fileName = $null, + $username = $null, + $password = $null, + [switch]$quiet +) + # Create a FTPWebRequest object to handle the connection to the ftp server + $ftprequest = [System.Net.FtpWebRequest]::create($url) + + # set the request's network credentials for an authenticated connection + $ftprequest.Credentials = + New-Object System.Net.NetworkCredential($username,$password) + + $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile + $ftprequest.UseBinary = $true + $ftprequest.KeepAlive = $false + + # send the ftp request to the server + $ftpresponse = $ftprequest.GetResponse() + [int]$goal = $ftpresponse.ContentLength + + # get a download stream from the server response + $reader = $ftpresponse.GetResponseStream() + + # create the target file on the local system and the download buffer + $writer = New-Object IO.FileStream ($fileName,[IO.FileMode]::Create) + [byte[]]$buffer = New-Object byte[] 1024 + [int]$total = [int]$count = 0 + + # loop through the download stream and send the data to the target file + do{ + $count = $reader.Read($buffer, 0, $buffer.Length); + $writer.Write($buffer, 0, $count); + if(!$quiet) { + $total += $count + if($goal -gt 0) { + Write-Progress "Downloading $url to $fileName" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100) + } else { + Write-Progress "Downloading $url to $fileName" "Saving $total bytes..." -id 0 -Completed + } + if ($total -eq $goal) { + Write-Progress "Completed download of $url." "Completed a total of $total bytes of $fileName" -id 0 -Completed + } + } + } while ($count -ne 0) + + $writer.Flush() + $writer.close() +} From 0304ee36873dea724be957ca1552d928b3add353 Mon Sep 17 00:00:00 2001 From: Stephen Austin Date: Thu, 9 Aug 2012 04:43:48 -0400 Subject: [PATCH 2/3] Revert "Added support for files located on FTP servers. Added new Get-FtpFile.ps1 script modeled after the existing Get-WebFile.ps1." This reverts commit 11e566ee1f252ca94af44dee9d4a31f1b49d1e42. --- .../functions/Get-ChocolateyWebFile.ps1 | 140 +++++++++--------- src/helpers/functions/Get-FtpFile.ps1 | 58 -------- 2 files changed, 69 insertions(+), 129 deletions(-) delete mode 100644 src/helpers/functions/Get-FtpFile.ps1 diff --git a/src/helpers/functions/Get-ChocolateyWebFile.ps1 b/src/helpers/functions/Get-ChocolateyWebFile.ps1 index 7e39e91..91b4246 100644 --- a/src/helpers/functions/Get-ChocolateyWebFile.ps1 +++ b/src/helpers/functions/Get-ChocolateyWebFile.ps1 @@ -1,72 +1,70 @@ -function Get-ChocolateyWebFile { -<# -.SYNOPSIS -Downloads a file from the internets. - -.DESCRIPTION -This will download a file from a url, tracking with a progress bar. -It returns the filepath to the downloaded file when it is complete. - -.PARAMETER PackageName -The name of the package we want to download - this is arbitrary, call it whatever you want. -It's recommended you call it the same as your nuget package id. - -.PARAMETER FileFullPath -This is the full path of the resulting file name. - -.PARAMETER Url -This is the url to download the file from. - -.PARAMETER Url64bit -OPTIONAL - If there is an x64 installer to download, please include it here. If not, delete this parameter - -.EXAMPLE -Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT' - -.NOTES -This helper reduces the number of lines one would have to write to download a file to 1 line. -There is no error handling built into this method. - -.LINK -Install-ChocolateyPackage -#> -param( - [string] $packageName, - [string] $fileFullPath, - [string] $url, - [string] $url64bit = $url -) - Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`',and url64bit:`'$url64bit`'"; - - $url32bit = $url; - $processor = Get-WmiObject Win32_Processor - $procCount=(Get-WmiObject Win32_ComputerSystem).NumberofProcessors - if ($procCount -eq '1') { - $is64bit = $processor.AddressWidth -eq 64 - Write-Debug "Processor width is $($processor.AddressWidth)." - } else { - Write-Debug "First processor width is $($processor[0].AddressWidth)." - $is64bit = $processor[0].AddressWidth -eq 64 - } - $systemBit = '32 bit' - if ($is64bit) { - $systemBit = '64 bit'; - $url = $url64bit; - } - - $downloadMessage = "Downloading $packageName ($url) to $fileFullPath" - if ($url32bit -ne $url64bit) {$downloadMessage = "Downloading $packageName $systemBit ($url) to $fileFullPath.";} - Write-Host "$downloadMessage" - #$downloader = new-object System.Net.WebClient - #$downloader.DownloadFile($url, $fileFullPath) - if ($url.StartsWith('http')) { - Get-WebFile $url $fileFullPath - } elseif ($url.StartsWith('ftp')) { - Get-FtpFile $url $fileFullPath - } else { - Write-Debug "We are attempting to copy the local item `'$url`' to `'$fileFullPath`'" - Copy-Item $url -Destination $fileFullPath -Force - } - - Start-Sleep 2 #give it a sec or two to finish up +function Get-ChocolateyWebFile { +<# +.SYNOPSIS +Downloads a file from the internets. + +.DESCRIPTION +This will download a file from a url, tracking with a progress bar. +It returns the filepath to the downloaded file when it is complete. + +.PARAMETER PackageName +The name of the package we want to download - this is arbitrary, call it whatever you want. +It's recommended you call it the same as your nuget package id. + +.PARAMETER FileFullPath +This is the full path of the resulting file name. + +.PARAMETER Url +This is the url to download the file from. + +.PARAMETER Url64bit +OPTIONAL - If there is an x64 installer to download, please include it here. If not, delete this parameter + +.EXAMPLE +Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT' + +.NOTES +This helper reduces the number of lines one would have to write to download a file to 1 line. +There is no error handling built into this method. + +.LINK +Install-ChocolateyPackage +#> +param( + [string] $packageName, + [string] $fileFullPath, + [string] $url, + [string] $url64bit = $url +) + Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`',and url64bit:`'$url64bit`'"; + + $url32bit = $url; + $processor = Get-WmiObject Win32_Processor + $procCount=(Get-WmiObject Win32_ComputerSystem).NumberofProcessors + if ($procCount -eq '1') { + $is64bit = $processor.AddressWidth -eq 64 + Write-Debug "Processor width is $($processor.AddressWidth)." + } else { + Write-Debug "First processor width is $($processor[0].AddressWidth)." + $is64bit = $processor[0].AddressWidth -eq 64 + } + $systemBit = '32 bit' + if ($is64bit) { + $systemBit = '64 bit'; + $url = $url64bit; + } + + $downloadMessage = "Downloading $packageName ($url) to $fileFullPath" + if ($url32bit -ne $url64bit) {$downloadMessage = "Downloading $packageName $systemBit ($url) to $fileFullPath.";} + Write-Host "$downloadMessage" + #$downloader = new-object System.Net.WebClient + #$downloader.DownloadFile($url, $fileFullPath) + if ($url.StartsWith('http')) { + Get-WebFile $url $fileFullPath + } else { + Write-Debug "We are attempting to copy the local item `'$url`' to `'$fileFullPath`'" + Copy-Item $url -Destination $fileFullPath -Force + } + + Start-Sleep 2 #give it a sec or two to finish up } \ No newline at end of file diff --git a/src/helpers/functions/Get-FtpFile.ps1 b/src/helpers/functions/Get-FtpFile.ps1 deleted file mode 100644 index 88743e6..0000000 --- a/src/helpers/functions/Get-FtpFile.ps1 +++ /dev/null @@ -1,58 +0,0 @@ -## Get-FtpFile -############################################################################################################## -## Downloads a file from ftp -## Some code from http://stackoverflow.com/questions/265339/whats-the-best-way-to-automate-secure-ftp-in-powershell -## Additional functionality emulated from http://poshcode.org/417 (Get-WebFile) -## Written by Stephen C. Austin, Pwnt & Co. http://pwnt.co -############################################################################################################## -function Get-FtpFile { -param( - $url = '', #(Read-Host "The URL to download"), - $fileName = $null, - $username = $null, - $password = $null, - [switch]$quiet -) - # Create a FTPWebRequest object to handle the connection to the ftp server - $ftprequest = [System.Net.FtpWebRequest]::create($url) - - # set the request's network credentials for an authenticated connection - $ftprequest.Credentials = - New-Object System.Net.NetworkCredential($username,$password) - - $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile - $ftprequest.UseBinary = $true - $ftprequest.KeepAlive = $false - - # send the ftp request to the server - $ftpresponse = $ftprequest.GetResponse() - [int]$goal = $ftpresponse.ContentLength - - # get a download stream from the server response - $reader = $ftpresponse.GetResponseStream() - - # create the target file on the local system and the download buffer - $writer = New-Object IO.FileStream ($fileName,[IO.FileMode]::Create) - [byte[]]$buffer = New-Object byte[] 1024 - [int]$total = [int]$count = 0 - - # loop through the download stream and send the data to the target file - do{ - $count = $reader.Read($buffer, 0, $buffer.Length); - $writer.Write($buffer, 0, $count); - if(!$quiet) { - $total += $count - if($goal -gt 0) { - Write-Progress "Downloading $url to $fileName" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100) - } else { - Write-Progress "Downloading $url to $fileName" "Saving $total bytes..." -id 0 -Completed - } - if ($total -eq $goal) { - Write-Progress "Completed download of $url." "Completed a total of $total bytes of $fileName" -id 0 -Completed - } - } - } while ($count -ne 0) - - $writer.Flush() - $writer.close() -} From 30fc5b25fe013f5ac1acaf85a9611e166ef46e4a Mon Sep 17 00:00:00 2001 From: Stephen Austin Date: Thu, 9 Aug 2012 04:47:37 -0400 Subject: [PATCH 3/3] Added support for files located on FTP servers. Added new Get-FtpFile.ps1 script modeled after the existing Get-WebFile.ps1 and integrated the function into Get-ChocolateyWebFile.ps1 so that URLs that begin with 'ftp' are processed using Get-FtpFile. --- .../functions/Get-ChocolateyWebFile.ps1 | 140 +++++++++--------- src/helpers/functions/Get-FtpFile.ps1 | 58 ++++++++ 2 files changed, 129 insertions(+), 69 deletions(-) create mode 100644 src/helpers/functions/Get-FtpFile.ps1 diff --git a/src/helpers/functions/Get-ChocolateyWebFile.ps1 b/src/helpers/functions/Get-ChocolateyWebFile.ps1 index 91b4246..7e39e91 100644 --- a/src/helpers/functions/Get-ChocolateyWebFile.ps1 +++ b/src/helpers/functions/Get-ChocolateyWebFile.ps1 @@ -1,70 +1,72 @@ -function Get-ChocolateyWebFile { -<# -.SYNOPSIS -Downloads a file from the internets. - -.DESCRIPTION -This will download a file from a url, tracking with a progress bar. -It returns the filepath to the downloaded file when it is complete. - -.PARAMETER PackageName -The name of the package we want to download - this is arbitrary, call it whatever you want. -It's recommended you call it the same as your nuget package id. - -.PARAMETER FileFullPath -This is the full path of the resulting file name. - -.PARAMETER Url -This is the url to download the file from. - -.PARAMETER Url64bit -OPTIONAL - If there is an x64 installer to download, please include it here. If not, delete this parameter - -.EXAMPLE -Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT' - -.NOTES -This helper reduces the number of lines one would have to write to download a file to 1 line. -There is no error handling built into this method. - -.LINK -Install-ChocolateyPackage -#> -param( - [string] $packageName, - [string] $fileFullPath, - [string] $url, - [string] $url64bit = $url -) - Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`',and url64bit:`'$url64bit`'"; - - $url32bit = $url; - $processor = Get-WmiObject Win32_Processor - $procCount=(Get-WmiObject Win32_ComputerSystem).NumberofProcessors - if ($procCount -eq '1') { - $is64bit = $processor.AddressWidth -eq 64 - Write-Debug "Processor width is $($processor.AddressWidth)." - } else { - Write-Debug "First processor width is $($processor[0].AddressWidth)." - $is64bit = $processor[0].AddressWidth -eq 64 - } - $systemBit = '32 bit' - if ($is64bit) { - $systemBit = '64 bit'; - $url = $url64bit; - } - - $downloadMessage = "Downloading $packageName ($url) to $fileFullPath" - if ($url32bit -ne $url64bit) {$downloadMessage = "Downloading $packageName $systemBit ($url) to $fileFullPath.";} - Write-Host "$downloadMessage" - #$downloader = new-object System.Net.WebClient - #$downloader.DownloadFile($url, $fileFullPath) - if ($url.StartsWith('http')) { - Get-WebFile $url $fileFullPath - } else { - Write-Debug "We are attempting to copy the local item `'$url`' to `'$fileFullPath`'" - Copy-Item $url -Destination $fileFullPath -Force - } - - Start-Sleep 2 #give it a sec or two to finish up +function Get-ChocolateyWebFile { +<# +.SYNOPSIS +Downloads a file from the internets. + +.DESCRIPTION +This will download a file from a url, tracking with a progress bar. +It returns the filepath to the downloaded file when it is complete. + +.PARAMETER PackageName +The name of the package we want to download - this is arbitrary, call it whatever you want. +It's recommended you call it the same as your nuget package id. + +.PARAMETER FileFullPath +This is the full path of the resulting file name. + +.PARAMETER Url +This is the url to download the file from. + +.PARAMETER Url64bit +OPTIONAL - If there is an x64 installer to download, please include it here. If not, delete this parameter + +.EXAMPLE +Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT' + +.NOTES +This helper reduces the number of lines one would have to write to download a file to 1 line. +There is no error handling built into this method. + +.LINK +Install-ChocolateyPackage +#> +param( + [string] $packageName, + [string] $fileFullPath, + [string] $url, + [string] $url64bit = $url +) + Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`',and url64bit:`'$url64bit`'"; + + $url32bit = $url; + $processor = Get-WmiObject Win32_Processor + $procCount=(Get-WmiObject Win32_ComputerSystem).NumberofProcessors + if ($procCount -eq '1') { + $is64bit = $processor.AddressWidth -eq 64 + Write-Debug "Processor width is $($processor.AddressWidth)." + } else { + Write-Debug "First processor width is $($processor[0].AddressWidth)." + $is64bit = $processor[0].AddressWidth -eq 64 + } + $systemBit = '32 bit' + if ($is64bit) { + $systemBit = '64 bit'; + $url = $url64bit; + } + + $downloadMessage = "Downloading $packageName ($url) to $fileFullPath" + if ($url32bit -ne $url64bit) {$downloadMessage = "Downloading $packageName $systemBit ($url) to $fileFullPath.";} + Write-Host "$downloadMessage" + #$downloader = new-object System.Net.WebClient + #$downloader.DownloadFile($url, $fileFullPath) + if ($url.StartsWith('http')) { + Get-WebFile $url $fileFullPath + } elseif ($url.StartsWith('ftp')) { + Get-FtpFile $url $fileFullPath + } else { + Write-Debug "We are attempting to copy the local item `'$url`' to `'$fileFullPath`'" + Copy-Item $url -Destination $fileFullPath -Force + } + + Start-Sleep 2 #give it a sec or two to finish up } \ No newline at end of file diff --git a/src/helpers/functions/Get-FtpFile.ps1 b/src/helpers/functions/Get-FtpFile.ps1 new file mode 100644 index 0000000..88743e6 --- /dev/null +++ b/src/helpers/functions/Get-FtpFile.ps1 @@ -0,0 +1,58 @@ +## Get-FtpFile +############################################################################################################## +## Downloads a file from ftp +## Some code from http://stackoverflow.com/questions/265339/whats-the-best-way-to-automate-secure-ftp-in-powershell +## Additional functionality emulated from http://poshcode.org/417 (Get-WebFile) +## Written by Stephen C. Austin, Pwnt & Co. http://pwnt.co +############################################################################################################## +function Get-FtpFile { +param( + $url = '', #(Read-Host "The URL to download"), + $fileName = $null, + $username = $null, + $password = $null, + [switch]$quiet +) + # Create a FTPWebRequest object to handle the connection to the ftp server + $ftprequest = [System.Net.FtpWebRequest]::create($url) + + # set the request's network credentials for an authenticated connection + $ftprequest.Credentials = + New-Object System.Net.NetworkCredential($username,$password) + + $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile + $ftprequest.UseBinary = $true + $ftprequest.KeepAlive = $false + + # send the ftp request to the server + $ftpresponse = $ftprequest.GetResponse() + [int]$goal = $ftpresponse.ContentLength + + # get a download stream from the server response + $reader = $ftpresponse.GetResponseStream() + + # create the target file on the local system and the download buffer + $writer = New-Object IO.FileStream ($fileName,[IO.FileMode]::Create) + [byte[]]$buffer = New-Object byte[] 1024 + [int]$total = [int]$count = 0 + + # loop through the download stream and send the data to the target file + do{ + $count = $reader.Read($buffer, 0, $buffer.Length); + $writer.Write($buffer, 0, $count); + if(!$quiet) { + $total += $count + if($goal -gt 0) { + Write-Progress "Downloading $url to $fileName" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100) + } else { + Write-Progress "Downloading $url to $fileName" "Saving $total bytes..." -id 0 -Completed + } + if ($total -eq $goal) { + Write-Progress "Completed download of $url." "Completed a total of $total bytes of $fileName" -id 0 -Completed + } + } + } while ($count -ne 0) + + $writer.Flush() + $writer.close() +}