Created
June 26, 2018 15:31
-
-
Save patrickhuber/5e44bd285e793e034ed8bcfdafd0b6dd to your computer and use it in GitHub Desktop.
download file powershell
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
$ErrorActionPreference = "Stop" | |
function Invoke-DownloadFileCore{ | |
param( | |
[string]$url, | |
[string]$output, | |
[bool]$skipTlsValidation, | |
[X509Certificate]$certificate) | |
$code = @' | |
using System; | |
using System.Collections.Generic; | |
using System.Net.Http; | |
using System.Net.Security; | |
using System.Security.Cryptography.X509Certificates; | |
public class TrustAllCertsUtility | |
{ | |
public static Func<HttpRequestMessage,X509Certificate2,X509Chain,SslPolicyErrors,Boolean> ValidationCallback = | |
(message, cert, chain, errors) => { | |
return true; | |
}; | |
} | |
'@ | |
Add-Type $code | |
$handler = [System.Net.Http.HttpClientHandler]::new() | |
if ($skipTlsValidation){ | |
$handler.ServerCertificateCustomValidationCallback = [TrustAllCertsUtility]::ValidationCallback | |
} | |
$client = [System.Net.Http.HttpClient]::new($handler) | |
$result = $client.GetAsync($url).Result | |
if (-not ($result.IsSuccessStatusCode)){ | |
throw [System.Exception] "Invalid response code. Expected 200 found $($result.StatusCode)" | |
} | |
$file = [System.IO.File]::Create($output) | |
$task = $result.Content.CopyToAsync($file) | |
$task.Result | Out-Null | |
$file.Close() | |
} | |
function Invoke-DownloadFileDesktop{ | |
param( | |
[string]$url, | |
[string]$output, | |
[bool]$skipTlsValidation, | |
[X509Certificate]$certificate) | |
add-type @" | |
using System.Net; | |
using System.Security.Cryptography.X509Certificates; | |
public class TrustAllCertsPolicy : ICertificatePolicy { | |
public bool CheckValidationResult( | |
ServicePoint srvPoint, X509Certificate certificate, | |
WebRequest request, int certificateProblem) { | |
return true; | |
} | |
} | |
"@ | |
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12' | |
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols | |
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
$wc = New-Object System.Net.WebClient | |
$wc.DownloadFile($url, $output) | |
} | |
function Invoke-DownloadFile{ | |
param( | |
[string]$url, | |
[string]$output, | |
[bool]$skipTlsValidation, | |
[X509Certificate]$certificate) | |
if ($PSEdition -eq 'Core'){ | |
return Invoke-DownloadFileCore -url $url -output $output -skipTlsValidation $skipTlsValidation -certificate $certificate | |
} | |
return Invoke-DownloadFileDesktop -url $url -output $output -skipTlsValidation $skipTlsValidation -certificate $certificate | |
} | |
Invoke-DownloadFile -url "https://packages.cloudfoundry.org/stable?release=windows64-exe&version=6.37.0&source=github-rel" -output "/tools/cf.zip" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment