Powershell script to download file, having trouble setting up a secure connection

StackOverflow https://stackoverflow.com/questions/146973

  •  02-07-2019
  •  | 
  •  

Question

I'm making an automated script to read a list from a site posting the latest compiled code. That's the part I've already figured out. The next part of the script is to grab that compiled code from a server with an Untrusted Cert.

This is how I'm going about grabbing the file:

$web = new-object System.Net.WebClient
$web.DownloadFile("https://uri/file.msi", "installer.msi")

Then I get the following error:

Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

I know I'm missing something, but I can't get the correct way to search for it.

Was it helpful?

Solution

You need to write a callback handler for ServicePointManager.ServerCertificateValidationCallback.

OTHER TIPS

Brad is correct, but notice that PowerShell V1 doesn't really have native support for delegates, which you'll need in this specific case. I believe this should get you around that limitation (in fact the scenario you describe is exactly one of the examples used).

If you are using powershell and face this error. Use command:

[Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

before downloading the package. It forces PS to use TLS 1.2.

The reason for the failure could be the website you are trying to download from has disabled the support for TLS 1.0 which PS uses by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top