Created
February 26, 2019 15:19
-
-
Save jmassardo/2e0dd7cce292f16ff8f6945b8b3752b5 to your computer and use it in GitHub Desktop.
PowerShell hack to ignore ssl certificates when using Invoke-WebRequest
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
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 | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12 | |
$Header = @{"api-token"="1234567890"} | |
$Request = Invoke-WebRequest -Uri "https://api.example.com" -Headers $Header -Method Get -ContentType "Application/Json" |
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
works on Windows + PS 5.1
If you just need to access some address, like running some sort of webhook, you can use curl:
$response = & curl.exe --insecure -X GET "$TriggerURL"
if ($response -notmatch "ok") {
...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@scarson - Aha yeah, so in practice it's kind of a pain, as the
System.Net.ICertificatePolicy
class does not exist at all for new age PowerShell, and code will not run at all if it's present in your script. In my particular case, I was using it in a module that is imported dynamically, so... here's how I'm using it, but I would suggest a good TLDR is don't bother, just use the C# snippet, it aint that bad 😅My module has a tree something like this
The, in PowerShell Core, errornous code is inside the PrivateModule.ps1 file.
The Public function that would use this Private function is in PublicFunction.ps1
And the import logic for this dynamic module is governed by the MyModule.psm1 file
Because the private function's name that won't execute in PowerShell Core is in the
$WindowsPowerShellOnly
array, when it gets to the checkif (($_.BaseName -in $WindowsPowerShellOnly)...
it won't be imported if it's not PSEdition Desktop.At the time I put that together I was determined to keep my codebase purely PowerShell, but having had time to think about it over time, I would in future simply do the PSEdition check and if it's Desktop, import the type using the C# snippet. But regardless, this code here is in use and working fine, it's just a level of complexity that isn't necessary and I think really only satisfied my curiosity and former stubbornness, but hey it's all part of the journey I guess.
Anyway, that's my answer to how I'm using that check in practice. Good luck and have fun!