Download file using Powershell script no working anymore due to URL change?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ITEngineer
Posts: 163
Joined: Wed Oct 12, 2011 10:52 am

Download file using Powershell script no working anymore due to URL change?

Post by ITEngineer » Wed Jul 03, 2019 9:01 pm

Hi,

Can anyone here please assist me what's wrong with the lines of code below?

Code: Select all

$Time = Get-Date -format 'F'
$DownloadDirectory = "C:\TEMP\JAVA"
$DownloadedFiles = Get-ChildItem -Path "$DownloadDirectory\*jre*.exe" -Force | Where-Object {!$_.PSIsContainer}

$DownloadedFiles | Remove-Item -Force

Try {
    $Link = (Invoke-WebRequest -UseBasicParsing –Uri 'https://www.java.com/en/download/manual.jsp').Links | Where-Object { $_.href -like "http*" } | Where-Object { $_.title -like "Download Java software for Windows (64-bit)" }
    Invoke-WebRequest $Link[0].href -OutFile "$($DownloadDirectory)\jre64x.exe"
    $Link = (Invoke-WebRequest -UseBasicParsing –Uri 'https://www.java.com/en/download/manual.jsp').Links | Where-Object { $_.href -like "http*" } | Where-Object { $_.title -like "Download Java software for Windows Offline" }
    Invoke-WebRequest $Link[0].href -OutFile "$($DownloadDirectory)\jre32x.exe"
}
Catch [Exception] {
    $ErrorMessage = $_.Exception
    Send-MailMessage -From "$env:COMPUTERNAME@$env:userdnsdomain" -To 'IT@domain.com' -Subject "Java Runtime Download Failed! - $($Error[0]) - as at $($Time)" -SmtpServer mail.domain.com -Body "The error message was $($ErrorMessage)"
    Break
}
Finally {
    Write-Host "Both Java Runtime $($DownloadedFiles) has been downloaded succesfully as at $($Time) and saved into $($DownloadDirectory)" -ForegroundColor Yellow
}
It used to be working and save the files in the directory as Jave RE engine offline components.

I got the below error message instead:
The error message was System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
Thanks in advance.
/* IT Engineer */

jvierra
Posts: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Download file using Powershell script no working anymore due to URL change?

Post by jvierra » Wed Jul 03, 2019 9:36 pm

You are likely not using TLS 1.2 as a protocol. Most web sites have disable SSL and TLS 1. Try using TLS 1.2.

User avatar
ITEngineer
Posts: 163
Joined: Wed Oct 12, 2011 10:52 am

Re: Download file using Powershell script no working anymore due to URL change?

Post by ITEngineer » Wed Jul 03, 2019 11:10 pm

jvierra wrote:
Wed Jul 03, 2019 9:36 pm
You are likely not using TLS 1.2 as a protocol. Most web sites have disable SSL and TLS 1. Try using TLS 1.2.
OK, so how do I implement that in the script above?
/* IT Engineer */

jvierra
Posts: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Download file using Powershell script no working anymore due to URL change?

Post by jvierra » Thu Jul 04, 2019 1:20 pm

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

User avatar
ITEngineer
Posts: 163
Joined: Wed Oct 12, 2011 10:52 am

Re: Download file using Powershell script no working anymore due to URL change?

Post by ITEngineer » Thu Jul 04, 2019 9:25 pm

jvierra wrote:
Thu Jul 04, 2019 1:20 pm
[Net.ServicePointManager]::SecurityProtocol = 'Tls12'
Nice, thank you for the pointer :-)
/* IT Engineer */

Locked