I'm kind of running out of options now...
Attempt 1
Use iwr in Powershell. It works, shows progress but its 10X slower and doesn't flush until while file is in memory :(.
powershell -command "& { iwr https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -OutFile SUV.zip }"
Attempt 2
Use .Net webclient in Powershell. It works but shows no progress and you can't terminate by Ctrl+C :(. Last issue is a big setback.
powershell -command "& { (New-Object System.Net.WebClient).DownloadFile('https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip', 'SUV.zip') }"
Attempt 3
Use BITS transfer in Powershell. It works, shows progress and almost perfect... until you find out that it mysteriously doesn't work on GitHub (errors out with 403 forbidden)!!
powershell -command "& { Start-BitsTransfer -Source https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -Destination SUV.zip }"
After some research I found @MichaelS's method the simplest, nevertheless I added some modifications that you could find useful, as such:
Try
/Finally
blocks, so we could handle user interruptions (Ctrl+C) and cleanup (See Gracefully stopping in Powershell)Write-Progress
functionality, so it feels more like Start-BitsTransfer
In action it looks like on that gif.
function Get-FileFromURL {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0)]
[System.Uri]$URL,
[Parameter(Mandatory, Position = 1)]
[string]$Filename
)
process {
try {
$request = [System.Net.HttpWebRequest]::Create($URL)
$request.set_Timeout(5000) # 5 second timeout
$response = $request.GetResponse()
$total_bytes = $response.ContentLength
$response_stream = $response.GetResponseStream()
try {
# 256KB works better on my machine for 1GB and 10GB files
# See https://www.microsoft.com/en-us/research/wp-content/uploads/2004/12/tr-2004-136.pdf
# Cf. https://stackoverflow.com/a/3034155/10504393
$buffer = New-Object -TypeName byte[] -ArgumentList 256KB
$target_stream = [System.IO.File]::Create($Filename)
$timer = New-Object -TypeName timers.timer
$timer.Interval = 1000 # Update progress every second
$timer_event = Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action {
$Global:update_progress = $true
}
$timer.Start()
do {
$count = $response_stream.Read($buffer, 0, $buffer.length)
$target_stream.Write($buffer, 0, $count)
$downloaded_bytes = $downloaded_bytes + $count
if ($Global:update_progress) {
$percent = $downloaded_bytes / $total_bytes
$status = @{
completed = "{0,6:p2} Completed" -f $percent
downloaded = "{0:n0} MB of {1:n0} MB" -f ($downloaded_bytes / 1MB), ($total_bytes / 1MB)
speed = "{0,7:n0} KB/s" -f (($downloaded_bytes - $prev_downloaded_bytes) / 1KB)
eta = "eta {0:hh\:mm\:ss}" -f (New-TimeSpan -Seconds (($total_bytes - $downloaded_bytes) / ($downloaded_bytes - $prev_downloaded_bytes)))
}
$progress_args = @{
Activity = "Downloading $URL"
Status = "$($status.completed) ($($status.downloaded)) $($status.speed) $($status.eta)"
PercentComplete = $percent * 100
}
Write-Progress @progress_args
$prev_downloaded_bytes = $downloaded_bytes
$Global:update_progress = $false
}
} while ($count -gt 0)
}
finally {
if ($timer) { $timer.Stop() }
if ($timer_event) { Unregister-Event -SubscriptionId $timer_event.Id }
if ($target_stream) { $target_stream.Dispose() }
# If file exists and $count is not zero or $null, than script was interrupted by user
if ((Test-Path $Filename) -and $count) { Remove-Item -Path $Filename }
}
}
finally {
if ($response) { $response.Dispose() }
if ($response_stream) { $response_stream.Dispose() }
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With