depreciated/Save-OSDProgressFile2.ps1
<#
.SYNOPSIS Download webfiles via Streamreader and updates progressbar every 4% .DESCRIPTION Download webfiles via Streamreader and updates progressbar every 4% .PARAMETER URL WebURL of download file .PARAMETER File Parameter description .EXAMPLE An example .NOTES General notes #> function Save-OSDProgressFile2 { [CmdletBinding()] param ( [Parameter(Mandatory)] [string]$URL, [Parameter(Mandatory)] [string]$DestinationFile ) Begin { # Check if UI is running ################################################### Stop-NoUI $fileName = $DestinationFile | Split-Path -Leaf Update-OSDProgress -DisplayBar -DownloadFile $fileName } Process { try { $storeEAP = $ErrorActionPreference $ErrorActionPreference = 'Stop' # Invoke request ################################################### $request = [System.Net.HttpWebRequest]::Create($URL) $response = $request.GetResponse() if ($response.StatusCode -eq 401 -or $response.StatusCode -eq 403 -or $response.StatusCode -eq 404) { throw "Remote file either doesn't exist, is unauthorized, or is forbidden for '$URL'." } if ($DestinationFile -match '^\.\\') { $DestinationFile = Join-Path (Get-Location -PSProvider "FileSystem") ($DestinationFile -Split '^\.')[1] } if ($DestinationFile -and !(Split-Path $DestinationFile)) { $DestinationFile = Join-Path (Get-Location -PSProvider "FileSystem") $DestinationFile } if ($DestinationFile) { $fileDirectory = $([System.IO.Path]::GetDirectoryName($DestinationFile)) if (!(Test-Path($fileDirectory))) { [System.IO.Directory]::CreateDirectory($fileDirectory) | Out-Null } } [long]$fullSize = $response.ContentLength $fullSizeMB = $fullSize / 1024 / 1024 # Define buffer ################################################### [byte[]]$buffer = new-object byte[] 1048576 [long]$total = [long]$count = 0 # create reader / writer $reader = $response.GetResponseStream() $writer = new-object System.IO.FileStream $DestinationFile, "Create" # Start download ################################################### $finalBarCount = 0 #show final bar only one time $lastPercent = 0 do { $count = $reader.Read($buffer, 0, $buffer.Length) $writer.Write($buffer, 0, $count) $total += $count $totalMB = $total / 1024 / 1024 $percent = $totalMB / $fullSizeMB [int]$percentComplete = $percent * 100 if ($fullSize -gt 0 -and ($percentComplete % 4 -eq 0) -and ($percentComplete -ne $lastPercent)) { # if (!$curFilename) { # Update-OSDProgress -DownloadFile $fileName # $curFilename = $fileName # } Update-OSDProgress -PercentComplete $percentComplete $lastPercent = $percentComplete } if ($total -eq $fullSize -and $count -eq 0 -and $finalBarCount -eq 0) { #dev Update-OSDProgress -PercentComplete 100 $finalBarCount++ } } while ($count -gt 0) } catch { $ExeptionMsg = $_.Exception.Message Write-Host "Download breaks with error : $ExeptionMsg" } finally { # Cleanup/Dispose ################################################### if ($reader) { $reader.Close() } if ($writer) { $writer.Flush(); $writer.Close() } $ErrorActionPreference = $storeEAP [GC]::Collect() } } End { Update-OSDProgress -HideBar -DownloadFile " " } } |