top of page

TLS and PowerShell Web Requests

  • Writer: Damian Scoles
    Damian Scoles
  • Jun 17, 2019
  • 2 min read


Apparently, TLS versions matter when working with PowerShell. It matters for PowerShell and Invoke-WebRequest commands. I have some schedule tasks that ran pulling small bits of information from them and sending email notifications on a daily basis. In the end, it’s a web scrape, find new info, send email. What I didn't pay attention to was the methodology (under the hood) of how those connections were made. The connections were defaulting to TLS 1.0. This was working up for quite some time and then the emails stopped being sent. ??


After some testing and experimentation, I was able to narrow it down to TLS/SSL. See below:


That error is what I receive when I attempted to run the Invoke-WebRequest cmdlet by itself and not scheduled. Well. That was quite helpful. With some searching, I was able to find a solution… So... How do we determine what the local machine is using for these PowerShell Invoke-WebRequests?


[Net.ServicePointManager]::SecurityProtocol

When we run them on different OS's we can see what the default available protocols are:


Windows Server 2016




Windows 10




Windows Server 2019





Notice how Windows Server 2019 and Window 10 have TLS12 available. Well, my task was schedule don a Windows 2016 server which only has SSL3 and TLS.


Solution


There are two ways to resolve an issue with TLS1.2 and PowerShell scripts. First is to move the scripts to a Windows 10 or Windows 2019 server. This would ensure that TLS12 would be available to a PowerShell session. The other way is to add these lines to any script that will perform a web request:


# Use TLS 1.2

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



Conclusion


So, what did we learn from this? Invoke-WebRequest's rely in underlying protocols to make a connection. Sometimes these protocols are not as updated as we think they are with the OS we are running. Also, troubleshooting a scheduled task may mean taking lines of code out of a script and running them manually to see if it’s the task itself, the code itself, or just certain lines. In our case, we were relying on an older protocol that web sites were no longer responding to or allowing connections from. With a bit of research, a workable solution was found and we can move on to other issues.

 
 
 

Recent Posts

See All
Null. Empty. Space? How to Detect?

** Disclaimer ** I do not know everything about PowerShell and I am sure I will miss some methods of detection of empty values. However,...

 
 
 

Comments


© 2016-2020 by Damian Scoles and Dave Stork.

bottom of page