Invoke-WebRequest to a web server with self-signed SSL certificate

Neo 421 Reputation points
2023-09-04T11:08:45.92+00:00

I have an IIS website hosted in a Windows 2019 server. I enbled HTTPS/SSL using a self-signed certificate.

Now in a client machine, I wanto use Invoke-WebRequest to connect to the server using HTTPS and it fails wjith no doubt.

Invoke-WebRequest: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch, RemoteCertificateChainErrors

Is there anyway to setup a custom authentication method so that I can bypass this restriction?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,621 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 35,416 Reputation points
    2023-09-04T14:02:48.5666667+00:00

    Add the -SkipCertificateCheck switch to the cmdlet.

    With PS 5, I got a SSL/TLS error. But on PS 7 I got your RemoteCertificateNameMismatch error. So if you are using PS 6 or 7 you can use SkipCertificateCheck. That is not implemented on PS 5.

    User's image

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,596 Reputation points
    2023-09-04T15:13:31.0966667+00:00

    If you want to give something else a try, put this in your script before using the Invoke-WebRequest cmdlet:

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
    

    It supplies a "$true" response to all certificate checks. FYI, the "callback" mentioned in the article is the script block at the end of the line.

    http://www.agarwalnishant.com/2014/07/ignore-ssl-certificate-check-in.html


  2. Koopee 0 Reputation points
    2025-02-06T15:02:33.66+00:00

    Battled with this too long, but found solution.

    If you just need to access some address, like running some sort of webhook, you can use curl, which is in windows10 by default.

    No need to set separate hoops to get around certificate checking by setting global callbacks.

    $response = & curl.exe --insecure -X GET "$TriggerURL"
    if ($response -notmatch "ok") {
      # Failed
      ...
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.