Tuesday, February 12, 2013

Powershell Script DIY: Error establishing connection when pulling HTML

I noticed on an early blog post titled “Downloading HTML code from the internet using powershell” I failed to touch on HTTPS connections with untrusted certificates.  Occasionally you may run up on a website that has an untrusted certificate that gives you this when you try to navigate to it using internet explorer:

This is because of a problem with the certificate being unverifiable.   Should you attempt to run a script that refers to this address it will return this error:

Exception calling "DownloadString" with "1" argument(s): "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
This can be alleviated by using the following line of code before you query the web page:

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

As always, do not query web pages unless you know the contents and do not run security code unless you know its effect.  Please leave you comment below!

Source: Daniel Richnak http://stackoverflow.com/questions/6781433/how-to-ignore-ssl-certificate-is-signed-by-an-unknown-certificate-authority-prob

No comments:

Post a Comment