DBremen
10/21/2015 - 9:30 AM

Download files using the DownloadFile method of the Network class within the Microsoft.VisualBasic.Devices Namespace

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
}