Downloading jdk using powershell

旧时模样 提交于 2020-02-20 04:03:21

问题


I am trying to download the java jdk using powershell scripting as given in the link below

http://poshcode.org/4224

. Here as the author has specified , if I Change the source url where the latest jdk is present i.e.,

http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-x64.exe

the content is not getting loaded , only about 6KB gets downloaded . I have a doubt , whether the download limit in powershell script is only 6KB?

Here is the code :

$source = "http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-i586.exe"
      $destination = "C:\Download\Java\jdk-7u60-windows-i586.exe"
      $client = new-object System.Net.WebClient
      $client.DownloadFile($source, $destination)

回答1:


On inspecting the session on oracle site the following cookie catches attention: oraclelicense=accept-securebackup-cookie. With that in mind you can run the following code:

$source = "http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-i586.exe"
$destination = "C:\Download\Java\jdk-7u60-windows-i586.exe"
$client = new-object System.Net.WebClient 
$cookie = "oraclelicense=accept-securebackup-cookie"
$client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie) 
$client.downloadFile($source, $destination)



回答2:


EDIT: here's the reason for your problem: you can't directly download the file without accepting the terms before.

I'm using the following script to download files. It's working with HTTP as well as with FTP. It might be a little overkill for your task because it also shows the download progress but you can trim it untill it fits your needs.

param(
    [Parameter(Mandatory=$true)]
    [String] $url,
    [Parameter(Mandatory=$false)]
    [String] $localFile = (Join-Path $pwd.Path $url.SubString($url.LastIndexOf('/'))) 
)

begin {
    $client = New-Object System.Net.WebClient
    $Global:downloadComplete = $false

    $eventDataComplete = Register-ObjectEvent $client DownloadFileCompleted `
        -SourceIdentifier WebClient.DownloadFileComplete `
        -Action {$Global:downloadComplete = $true}
    $eventDataProgress = Register-ObjectEvent $client DownloadProgressChanged `
        -SourceIdentifier WebClient.DownloadProgressChanged `
        -Action { $Global:DPCEventArgs = $EventArgs }    
}

process {
    Write-Progress -Activity 'Downloading file' -Status $url
    $client.DownloadFileAsync($url, $localFile)

    while (!($Global:downloadComplete)) {                
        $pc = $Global:DPCEventArgs.ProgressPercentage
        if ($pc -ne $null) {
            Write-Progress -Activity 'Downloading file' -Status $url -PercentComplete $pc
        }
    }

    Write-Progress -Activity 'Downloading file' -Status $url -Complete
}

end {
    Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged
    Unregister-Event -SourceIdentifier WebClient.DownloadFileComplete
    $client.Dispose()
    $Global:downloadComplete = $null
    $Global:DPCEventArgs = $null
    Remove-Variable client
    Remove-Variable eventDataComplete
    Remove-Variable eventDataProgress
    [GC]::Collect()    
}



回答3:


After needing this script for a platform installer that required the JDK as a dependency:

<# Config #>
$DownloadPageUri = 'https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html';
$JavaVersion = '8u201';

<# Build the WebSession containing the proper cookie 
   needed to auto-accept the license agreement. #>
$Cookie=New-Object -TypeName System.Net.Cookie; 
$Cookie.Domain='oracle.com';
$Cookie.Name='oraclelicense';
$Cookie.Value='accept-securebackup-cookie'; 
$Session= `
   New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession;
$Session.Cookies.Add($Cookie);

<# Fetch the proper Uri and filename from the webpage. #>
$JdkUri = (Invoke-WebRequest -Uri $DownloadPageUri -WebSession $Session -UseBasicParsing).RawContent `
    -split "`n" | ForEach-Object { `
        If ($_ -imatch '"filepath":"(https://[^"]+)"' { `
            $Matches[1] `
        } `
    } | Where-Object { `
        $_ -like "*-$JavaVersion-windows-x64.exe" `
    }[0];
If ($JdkUri -imatch '/([^/]+)$') { 
    $JdkFileName=$Matches[1];
}

<# Use a try/catch to catch the 302 moved temporarily 
   exception generated by oracle from the absance of
   AuthParam in the original URL, piping the exception's
   AbsoluteUri to a new request with AuthParam returned
   from Oracle's servers.  (NEW!) #>

try { 
    Invoke-WebRequest -Uri $JdkUri -WebSession $Session -OutFile "$JdkFileName"
} catch { 
    $authparam_uri = $_.Exception.Response.Headers.Location.AbsoluteUri;
    Invoke-WebRequest -Uri $authparam_uri -WebSession $Session -OutFile "$JdkFileName"
} 

Works in PowerShell 6.0 (Hello 2019!) Required a minor fixup to the regex and the way the -imatch line scan happened. Workaround to follow 302 redirects was located here: https://github.com/PowerShell/PowerShell/issues/2896 (302 redirect workaround by fcabralpacheco readapted to get Oracle downloads working again !

This solution downloads 8u201 for Windows x64 automatically.




回答4:


Since the accepted comment is not working anymore and I cannot comment, I will leave my method here. I modified @steve-coleman's answer by making it scan the page for download links that start with https instead of http. Also I needed to modify $Response to have a -UseBasicParsing flag as I was using a headless Windows Server. All in all the script I used was:

$DownloadPageUri = 'http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html'
$JavaVersion = '8u192'

$Response = Invoke-WebRequest -Uri $DownloadPageUri -UseBasicParsing

$JreUri = @($Response.RawContent -split "`n" | ForEach-Object {If ($_ -imatch '"filepath":"(https://[^"]+)"') {$Matches[1]}} | Where-Object {$_ -like "*-$JavaVersion-windows-x64.tar.gz"})
If (-Not $JreUri.Count -eq 1) {
    throw ('Expected to retrieve only one URI but got {0}' -f $JreUri.Count)
}

If ($JreUri[0] -imatch '/([^/]+)$') {
    $JreFileName = $Matches[1]
}
$JreFileName = 'jre-windows-x64.tar.gz'

If (-Not (Test-Path -Path "$PSScriptRoot\$JreFileName") -Or -Not (Test-Path -Path "$PSScriptRoot\Java-$JavaVersion.tag")) {
    $Cookie  = New-Object -TypeName System.Net.Cookie
    $Cookie.Domain = 'oracle.com'
    $Cookie.Name   = 'oraclelicense'
    $Cookie.Value  = 'accept-securebackup-cookie'
    $Session = New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession
    $Session.Cookies.Add($Cookie)
    Invoke-WebRequest -Uri $JreUri[0] -WebSession $Session -OutFile "$PSScriptRoot\$JreFileName"
    New-Item -Path "$PSScriptRoot\Java-$JavaVersion.tag" -ItemType File | Out-Null
}

As usual, YMMV, especially if Oracle change their website.




回答5:


I modified Robert Smith's answer as follows to get SDK 8u211

<# Config #>
$DownloadPageUri = 'https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html';
$JavaVersion = '8u211';
<# Build the WebSession containing the proper cookie
   needed to auto-accept the license agreement. #>

$Cookie=New-Object -TypeName System.Net.Cookie;
$Cookie.Domain='oracle.com';
$Cookie.Name='oraclelicense';
$Cookie.Value='accept-securebackup-cookie';
$Session= `
   New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession;
$Session.Cookies.Add($Cookie);

# Fetch the proper Uri and filename from the webpage. #>
$i = 0
#echo $i
#$rc = (Invoke-WebRequest -Uri $DownloadPageUri -WebSession $Session     -UseBasicParsing).RawContent
#echo $rc >> Oracle.html
#-split "`n" | ForEach-Object { `
$JdkUri = (Invoke-WebRequest -Uri $DownloadPageUri -WebSession $Session -UseBasicParsing).RawContent `
-split '\r?\n' | ForEach-Object { `
    #echo "$i : $_" >> Oracle.html
    #echo "$i"
    #$i++
    If ($_ -imatch '"filepath":"(https://[^"]+)"') { `
        $Matches[1] `
    } `
} | Where-Object { `
    $_ -like "*-$JavaVersion-windows-x64.exe" `
}[0];
If ($JdkUri -imatch '/([^/]+)$') {
    $JdkFileName=$Matches[1];
}
echo "JdkUri: $JdkUri" >> out.txt

<# Use a try/catch to catch the 302 moved temporarily
   exception generated by oracle from the absance of
   AuthParam in the original URL, piping the exception's
   AbsoluteUri to a new request with AuthParam returned
   from Oracle's servers.  (NEW!) #>

try {
    echo "In the try" >> out.txt
    Invoke-WebRequest -Uri $JdkUri -WebSession $Session -OutFile "$JdkFileName"
} catch {
    echo "In the catch" >> out.txt
    $authparam_uri = $_.Exception.Response.Headers.Location.AbsoluteUri;
    Invoke-WebRequest -Uri $authparam_uri -WebSession $Session -OutFile "$JdkFileName"
}

Had to change the line -split "n" | ForEach-Object { to -split '\r?\n' | ForEach-Object { `

And the line If ($_ -imatch '"filepath":"(https://[^"]+)"' { to If ($_ -imatch '"filepath":"(https://[^"]+)"') {

But in the jkd-8u211-windows-x64.exe file I see

<html>
<script language="javascript" type="text/javascript">
function submitForm()
{
    var hash = location.hash;
    if (hash) {
        if(hash.indexOf("#") == -1){
            hash="#"+hash
        }
        document.myForm.action = document.myForm.action+hash;
    }
    document.myForm.submit();
}
</script><head><base target="_self"></head><body onLoad="submitForm()"><noscript>       <p>JavaScript is required. Enable JavaScript to use OAM Server.</p></noscript><form   action="https://login.oracle.com/mysso/signon.jsp" method="post" name="myForm"> <!------------ DO NOT REMOVE -------------><!----- loginform renderBrowserView -----><!-- Required for SmartView Integration --><input type="hidden" name="bmctx" value="05C65B855F507509F2A50E4187F746B908003F815E8CAC65124394831AC5D9EF"><input type="hidden" name="contextType" value="external"><input type="hidden" name="username" value="string"><input type="hidden" name="contextValue" value="%2Foam"><input type="hidden" name="password" value="secure_string"><input type="hidden" name="challenge_url" value="https%3A%2F%2Flogin.oracle.com%2Fmysso%2Fsignon.jsp"><input type="hidden" name="request_id" value="-7095362132919578964"><input type="hidden" name="authn_try_count" value="0"><input type="hidden" name="OAM_REQ" value="VERSION_4~FWMjicP9PEQpljiEhsObqw9ylPjCAidBVTTJxhmD1hxQnGA4pyWKyCqsJWbqRCHi7qV9ZGSvNoA5t9c33sgIg2B%2bVMyE%2b7Ev3v%2f0UWTE5TDtvGOxisL8d7gDA5piF4HZHubed6GfNVCktpGfz0y7KW0roRz3%2b58adfBEomJffDOl1UAkCDbV%2b7712R6CjE6nfODUMJTFUJ6nc9Zq6%2fuooydHQx9GjTeD6RBSlDo2L2b0KDi5g1exggoqrUMintE3QRbymUgvxkJyVAoxC%2bo8xLDF4EOICI0vkYnovHUNN%2bNoiQLGWXv1ryOuFy9FIebX%2bA5uxiDbulaRSDQcGO1LefWYF%2bBP7Vo3wRcNb%2bgc8J9%2fRjGVlKQSETLkUXd%2fo0U7cEPFSrq1qOmE6B7yKkB3zFnIsxHELMwdhVD5WJ95xebDem9xlhv61ImKJH3JdrnnDYZ3e3S1v90epLpFQyG1BExZxc%2fVLQZt%2bAexfGNstpqmlCFnfuC4Xudh0omOLAz5ZCdeWReNh0rC%2f0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Et%2bjM0AuDStGxf1ve3bzkWTtu6GLjGQFCf4Zlq7Q%2f12eMCype4lCnFwL8OA7J7LakMUK8miUiiEgJDfcLsRYDVa4amcR6uUo2E3GueUa4KXXH6XZQg%2fvH6JmzvP%2bLIKx%2fyMWf6JuKdi6w6P34VTBPLi1o9rQVTeIMQB0NsvQWowWcZEm1eGLC1Q%2fVJFMUHTCF9S75hL%2bYSve%2blE2JKf4Fbxp%2fAEGsuHmfRNu8lWHzaNuuFRZwl0mt8OnnmPAhOYpb1fvqkGLQfFRRYF1H7S1zFcTGeFalXa9FeuAQ31xglHgoXYni6TJOSQWbKAEiyh6YjwPza2U0hcaS%2fLbJwblHbhx8dko3My7kED75iKKmfUsvWO%2fxE93ngbP%2bNC8vs7KVzKyJhoKamHYykXfqWCWlCAz%2fyzo2GgU1ZAIdTHQOrysMnTIZxQvm9oOK8egdTbrweyHjsLYNGvA8LbmutI8%2fbtAD5BS9IE02qL2YtmI3nT39srKW%2b8JEP727dtLDGPaFJGDmfszhXZu1elSKEOUa62O8QoIjt5znJaVfJgh3sNrvj6VgGj3WGZf5EVMeD6LUdrQEtv8G3ZIkL1p%2f3qBSlgDjEPjDcTUJtEYNjuly7FNyBq2ZEgwvIjXIB8Q8nId%2b1QPOygTfkM5gqFlK2tG0sRpZEIh6J9Ov1IsgKBTBvfGvGuwI3REk2WXMkt78l7JrS3zvQoXpbM4e724JosN7LnrLQ%2bxn%2f6Ofy4RGNDza9FpGkcXea9%2b5%2fnNEKDNaYl3hZiTS2YxYTDFSKkotkadA3BJBse9dMF1cC5bj3IKgiNrTgSpHOvZbIEVi7bRJg7x66lzWWuellSEd%2bZYpIdxdBqUGrF7KehTh%2fHsjG5T5678e%2fuT8Uvm9S7bmE6xY3tBlQ2aO5FKWIA%3d%3d"><input type="hidden" name="locale" value="en"><input type="hidden" name="resource_url" value="https%253A%252F%252Fedelivery.oracle.com%252Fakam%252Fotn%252Fjava%252Fjdk%252F8u211-b12%252F478a62b7d4e34b78b671c754eaaf38ab%252Fjdk-8u211-windows-x64.exe"></form></body>    </html>

Not sure what to do next. Also added a bit of debug jazz like #echo "$i : $_" >> Oracle.html ...




回答6:


I got this here and all credit goes to him.... http://dille.name/blog/2016/06/21/running-minecraft-in-a-windows-container-using-docker/

https://github.com/nicholasdille/docker

It works and downloads the Java files.

$DownloadPageUri = 'http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html'
$JavaVersion = '8u172'

$Response = Invoke-WebRequest -Uri $DownloadPageUri

$JreUri = @($Response.RawContent -split "`n" | ForEach-Object {If ($_ -imatch '"filepath":"(http://[^"]+)"') {$Matches[1]}} | Where-Object {$_ -like "*-$JavaVersion-windows-x64.tar.gz"})
If (-Not $JreUri.Count -eq 1) {
    throw ('Expected to retrieve only one URI but got {0}' -f $JreUri.Count)
}

If ($JreUri[0] -imatch '/([^/]+)$') {
    $JreFileName = $Matches[1]
}
$JreFileName = 'jre-windows-x64.tar.gz'

If (-Not (Test-Path -Path "$PSScriptRoot\$JreFileName") -Or -Not (Test-Path -Path "$PSScriptRoot\Java-$JavaVersion.tag")) {
    $Cookie  = New-Object -TypeName System.Net.Cookie
    $Cookie.Domain = 'oracle.com'
    $Cookie.Name   = 'oraclelicense'
    $Cookie.Value  = 'accept-securebackup-cookie'
    $Session = New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession
    $Session.Cookies.Add($Cookie)
    Invoke-WebRequest -Uri $JreUri[0] -WebSession $Session -OutFile "$PSScriptRoot\$JreFileName"
    New-Item -Path "$PSScriptRoot\Java-$JavaVersion.tag" -ItemType File | Out-Null
}


来源:https://stackoverflow.com/questions/24430141/downloading-jdk-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!