Skip to content

Instantly share code, notes, and snippets.

@DBremen
Last active August 16, 2022 03:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DBremen/506ef86d0525b0eaf6ab to your computer and use it in GitHub Desktop.
Save DBremen/506ef86d0525b0eaf6ab to your computer and use it in GitHub Desktop.
Download files using the DownloadFile method of the Network class within the Microsoft.VisualBasic.Devices Namespace
function Get-FileVB{
param(
[Parameter(Mandatory=$true)]
$url,
$destinationFolder="$env:USERPROFILE\Downloads",
[switch]$includeStats
)
Add-Type -AssemblyName Microsoft.VisualBasic
#resolve potential redirect
$response = [System.Net.WebRequest]::Create($url).GetResponse()
$url = $response.ResponseUri.OriginalString
$response.Close()
$destination = Join-Path $destinationFolder ($url | Split-Path -Leaf)
$net = New-Object Microsoft.VisualBasic.Devices.Network
$start = Get-Date
#signature DownloadFile(url, destination, username, password, [bool]showUI, [int]connecdtionTimeOutInMS,[Microsoft.VisualBasic.FileIO.UICancelOption]OnUserCancel)
$net.DownloadFile($url, $destination, '', '', $true, 500, [Microsoft.VisualBasic.FileIO.UICancelOption]::DoNothing )
$elapsed = ((Get-Date) - $start).ToString('hh\:mm\:ss')
$totalSize = (Get-Item $destination).Length | Get-FileSize
if ($includeStats.IsPresent){
[PSCustomObject]@{Name=$MyInvocation.MyCommand;TotalSize=$totalSize;Time=$elapsed}
}
Get-Item $destination | Unblock-File
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment