Failed to download from Internet

The following codes are downloading a text file from the Internet. It looks simple and straight forward.

$u2 = "https://my-test-web.com/my-file.txt"
$f2 = "my-file.txt"
Invoke-WebRequest -Uri $u2 -OutFile $f2


The above code looks good and we never expect download failure except that Internet connection has broken down.

But, if you look at the value of $u2, you might notice that it is calling HTTPS which require Powershell to validate the SSL certificate. So, there is a chance for the above code failed when the web server disabled Tls v1.1 and below.

To resolve/avoid this issue, you must change the environment setting first before calling Invoke-WebRequest.

# enforce Tls1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12


$u2 = "https://my-test-web.com/my-file.txt"
$f2 = "my-file.txt"
Invoke-WebRequest -Uri $u2 -OutFile $f2



Comments

Popular posts from this blog

Scheduled task in Windows Task Scheduler - part 3

Create scheduled task in Windows Task Scheduler

Get all website entries in IIS