Last active
August 16, 2022 03:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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