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

Ask your 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.
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

[Net.ServicePointManager]::SecurityProtocol = 'Tls12'
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

jvierra wrote: Thu Jul 04, 2019 1:20 pm [Net.ServicePointManager]::SecurityProtocol = 'Tls12'
Nice, thank you for the pointer :-)
/* IT Engineer */
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked