Page 1 of 1

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

Posted: Wed Jul 03, 2019 9:01 pm
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.

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

Posted: Wed Jul 03, 2019 9:36 pm
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.

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

Posted: Wed Jul 03, 2019 11:10 pm
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?

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

Posted: Thu Jul 04, 2019 1:20 pm
by jvierra
[Net.ServicePointManager]::SecurityProtocol = 'Tls12'

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

Posted: Thu Jul 04, 2019 9:25 pm
by ITEngineer
jvierra wrote:
Thu Jul 04, 2019 1:20 pm
[Net.ServicePointManager]::SecurityProtocol = 'Tls12'
Nice, thank you for the pointer :-)