Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel

Tags:

powershell

ssl

I'm trying to execute this powershell command

Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

and I get this error. "Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel." https requests appear to work ("https://google.com") but not this one in question. How can I get this to work or use other powershell command to read the page contents?

like image 855
hewstone Avatar asked Jan 12 '17 16:01

hewstone


2 Answers

try using this one

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri https://apod.nasa.gov/apod/ 
like image 172
Chandan Rai Avatar answered Sep 19 '22 16:09

Chandan Rai


In a shameless attempt to steal some votes, SecurityProtocol is an Enum with the [Flags] attribute. So you can do this:

[Net.ServicePointManager]::SecurityProtocol =    [Net.SecurityProtocolType]::Tls12 -bor `   [Net.SecurityProtocolType]::Tls11 -bor `   [Net.SecurityProtocolType]::Tls 

Or since this is PowerShell, you can let it parse a string for you:

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls" 

Then you don't technically need to know the TLS version.

I copied and pasted this from a script I created after reading this answer because I didn't want to cycle through all the available protocols to find one that worked. Of course, you could do that if you wanted to.

Final note - I have the original (minus SO edits) statement in my PowerShell profile so it's in every session I start now. It's not totally foolproof since there are still some sites that just fail but I surely see the message in question much less frequently.

like image 20
No Refunds No Returns Avatar answered Sep 19 '22 16:09

No Refunds No Returns