Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell iwr fails when attempting -SkipCertificateCheck

Tags:

powershell

Using powershell (version 5.1.18362.145) and attempting to use Invoke-WebRequest it fails when using the -SkipCertificateCheck.

I don't know what to do about this as it seems to be documented on the msdn. I have tried running Update-Module just in case the module was an old version however that has not rectified the issue.

Command:

iwr -SkipCertificateCheck google.com -UseBasicParsing -Method Head

Error:

Invoke-WebRequest : A parameter cannot be found that matches parameter name 'SkipCertificateCheck'

At line:1 char:5
+ iwr -SkipCertificateCheck google.com -Method Head
+     ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand`

Does anyone know how to get Invoke-WebRequest to work without checking certificates?

The overall goal of this is to use Invoke-WebRequest with a site that has a self signed certificate.

like image 594
Dennis43589y Avatar asked Jan 27 '20 00:01

Dennis43589y


People also ask

What is Iwr in PowerShell?

* The iwr (Invoke-WebRequest) reads the text file from the uri. * The ConvertFrom-Json cmdlet reads the content as json and parses it according to json rules. The output is a sequence of objects (not json objects, but PowerShell objects) with properties corresponding to the json fields.

How do you fix the term is not recognized as the name of a cmdlet in Windows PowerShell?

If that module is missing, corrupt, or got moved, it throws up the error, “the term is not recognized as the name of a cmdlet.” You can use “get-module” in PowerShell to see if the module is present and correct. It will show you what modules are loaded, and you can add or repair them depending on your needs.

How do I throw an exception in PowerShell?

To create our own exception event, we throw an exception with the throw keyword. This creates a runtime exception that is a terminating error. It's handled by a catch in a calling function or exits the script with a message like this.

What is the difference between invoke RestMethod and invoke WebRequest?

Invoke-RestMethod is perfect for quick APIs that have no special response information such as Headers or Status Codes, whereas Invoke-WebRequest gives you full access to the Response object and all the details it provides.


1 Answers

SkipCertificateCheck is not available on 5.1, you're most likely looking at the wrong version of PowerShell. This is a common workaround used for Untrusted Certificates.

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
Invoke-WebRequest https://expired.badssl.com/
like image 145
jfrmilner Avatar answered Oct 12 '22 15:10

jfrmilner