简体   繁体   中英

Download Java x64 and Microsoft Edge with PowerShell

I found this script to download Javax64 and it really works, but I had some problems.

The first is that the command I would put would be inside an XML file that a powershell script calls it, so putting it directly like this, it gave some errors because where it shows "<a" the XML understood that this was part of it and not of something that only PowerShell would make use of.

The second is "New-Object -ComObject "InternetExplorer.Application" where this is not working on my Windows Server and it is recommended not to use it for ie it is being discontinued soon. It still works on Windows 10 normally but on Windows Server it gets stuck in a loop and won't get out.

How would I convert this script to an Invoke-WebRequest, is this possible? Because then I would just need to put the complete string of the Invoke-WebResquest in my XML file and PowerShell would read it normally, I think.

$ie = New-Object -ComObject "InternetExplorer.Application"
# Navigate to the requested page
$ie.Navigate2("https://www.java.com/en/download/manual.jsp")

$anchor = $null
while($anchor -eq $null -or $anchor -eq "")
{
     #wait 1 second for the page to load
     start-sleep -m 1000
     #get the html of the page
     $html = $ie.document.body.innerHTML
     #apply your regex to identify the anchar with the download link
     $anchor = [regex]::Match($html, '(?:<a title="Download Java software for Windows \(64-bit\)" href=")(.*)(?:">)').Groups[1] .Value
}

#regex doesn't return the link correctly, that's why I made the substring to get the link
$url_download = $anchor.Substring(0,$anchor.IndexOf(""""))
$url_download

Edit: There is the same situation but to download Edge?

Looking at the oraclejdk Chocolatey package installation script, the URL is https://download.oracle.com/java/17/archive/jdk-17.0.2_windows-x64_bin.msi . Since you're already familiar with Chocolatey from another environment, I would figure the version you need, see if there is a Chocolatey package for it, and get the direct URL from that package version's installation script.

You could also attempt to templatize the URL like so:

https://download.oracle.com/java/MAJOR_VERSION/archive/jdk-MAJOR_VERSION.MINOR_VERSION.PATCH_windows-x64_bin.msi

where MAJOR_VERSION , MINOR_VERSION , and PATCH are pieces of the Java version. However, I have not tested that all Java MSI URLs follow this pattern.


Regardless, once you have the URL, it's as simple as:

# Work around performance issue with iwr and the progress bar
$ProgressPreference = 'SilentlyContinue'

$MSI_URL = 'https://download.oracle.com/java/17/archive/jdk-17.0.2_windows-x64_bin.msi'
Invoke-WebRequest -UseBasicParsing $MSI_URL -Outfile 'jdk-17.0.2_windows-x64_bin.msi'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM