The Problem:
Sometimes PowerShell ISE returns the following TLS-triggered error when one implements Invoke-WebRequest or Invoke-RestMethod during a session:
“The underlying connection was closed: An unexpected error occurred on a receive…” ( https://stackoverflow.com/questions/66050990/the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-receive ).
The Solution:
To avoid said error, enable TLS (Transport Layer Security, https://docs.microsoft.com/en-us/microsoft-365/compliance/prepare-tls-1.2-in-office-365?view=o365-worldwide ) by adding the following line before using Invoke-WebRequest (or Invoke-RestMethod):
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
For instance, to retrive URIs from mit.edu, use
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
(Invoke-WebRequest -Uri ‘mit.edu’).links.href
Similarly, to retrieve URIs from Google search results for the [corona virus] query,…