Powershell v3 Invoke-WebRequest HTTPS error
This work-around worked for me:http://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors
Basically, in your PowerShell script:
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; } }"@[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy$result = Invoke-WebRequest -Uri "https://IpAddress/resource"
Lee's answer is great, but I also had issues with which protocols the web server supported.
After also adding the following lines, I could get the https request through. As pointed out in this answer https://stackoverflow.com/a/36266735
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
My full solution with Lee's code.
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
An alternative implementation in pure powershell (without Add-Type
of c# source):
#requires -Version 5#requires -PSEdition Desktopclass TrustAllCertsPolicy : System.Net.ICertificatePolicy { [bool] CheckValidationResult([System.Net.ServicePoint] $a, [System.Security.Cryptography.X509Certificates.X509Certificate] $b, [System.Net.WebRequest] $c, [int] $d) { return $true }}[System.Net.ServicePointManager]::CertificatePolicy = [TrustAllCertsPolicy]::new()