Created
November 3, 2019 21:02
-
-
Save joncloud/5f3307fe2909d60bfa410f880ef13c74 to your computer and use it in GitHub Desktop.
This file contains hidden or 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-HttpFile { | |
| param( | |
| [Parameter(Mandatory = $true)] | |
| [string] | |
| $Uri, | |
| [Parameter(Mandatory = $true)] | |
| [string] | |
| $OutFile, | |
| [switch] | |
| $Asynchronous = $False, | |
| [switch] | |
| $Force = $False | |
| ) | |
| If (Test-Path $OutFile) { | |
| If ($Force) { | |
| Remove-Item $OutFile -Force | Out-Null | |
| } | |
| Else { | |
| Throw "${OutFile} already exists. If you want to remove it, use -Force." | |
| } | |
| } | |
| $Directory = Split-Path $OutFile | |
| If (-not (Test-Path $Directory)) { | |
| Write-Information 'Creating target directory' $Directory | |
| New-Item $Directory -Type Directory | Out-Null | |
| } | |
| $OutFile = Join-Path (Resolve-Path $Directory) (Split-Path $OutFile -Leaf) | |
| Function Get-HttpFileBits { | |
| Try { | |
| Write-Information 'Trying to use BITS Transfer' | |
| If ($Asynchronous) { | |
| Start-BitsTransfer -Source $Uri -Destination $OutFile -Asynchronous -ErrorAction SilentlyContinue | Out-Null | |
| } | |
| Else { | |
| Start-BitsTransfer -Source $Uri -Destination $OutFile -ErrorAction SilentlyContinue | Out-Null | |
| } | |
| $True | |
| } | |
| Catch { | |
| Write-Warning 'Failed to use BITS Transfer' $_ | |
| $False | |
| } | |
| } | |
| Function Get-HttpFileWebClient { | |
| Write-Information 'Using System.Net.WebClient' | |
| If ($Asynchronous) { | |
| (New-Object System.Net.WebClient).DownloadFileAsync($Uri, $OutFile) | Out-Null | |
| } | |
| Else { | |
| (New-Object System.Net.WebClient).DownloadFile($Uri, $OutFile) | Out-Null | |
| } | |
| } | |
| If (-not (Get-HttpFileBits)) { | |
| Get-HttpFileWebClient | Out-Null | |
| } | |
| } | |
| Export-ModuleMember Get-HttpFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment