使用PowerShell下载jdk

时间:2014-06-26 12:08:20

标签: java powershell powershell-v2.0

我正在尝试使用下面链接中给出的powershell脚本来下载java jdk

http://poshcode.org/4224

。这是作者指定的,如果我更改了最新jdk所在的源URL 即,

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

内容未加载,只下载约6KB。我有一个疑问,PowerShell脚本中的下载限制是否只有6KB?

以下是代码:

$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)

6 个答案:

答案 0 :(得分:9)

在oracle网站上检查会话时,以下cookie引起了注意:oraclelicense=accept-securebackup-cookie。考虑到这一点,您可以运行以下代码:

$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)

答案 1 :(得分:4)

编辑:这就是问题的原因:您无法直接下载该文件,而无需接受条款

我使用以下脚本下载文件。它使用HTTP和FTP。这对你的任务来说可能有点过分,因为它也显示了下载进度,但你可以修剪它,直到它符合你的需要。

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()    
}

答案 2 :(得分:0)

由于接受的评论不再起作用,并且我无法发表评论,因此我将在这里保留我的方法。我修改了@ steve-coleman的答案,使其扫描页面以https而不是http开头的下载链接。同样,在使用无头Windows Server时,我还需要修改$Response以具有-UseBasicParsing标志。我使用的所有脚本总共是:

$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
}

YMMV像往常一样,特别是如果Oracle更改其网站。

答案 3 :(得分:0)

在需要将JDK作为依赖项的平台安装程序需要此脚本之后:

<# 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"
} 

在PowerShell 6.0中工作(Hello 2019!)需要对正则表达式和-imatch行扫描的方式进行较小的修复。遵循302重定向的解决方法位于此处:https://github.com/PowerShell/PowerShell/issues/2896(fcabralpacheco的302重定向解决方法已重新配置为使Oracle下载再次正常工作!

此解决方案自动为Windows x64下载8u201。

答案 4 :(得分:0)

我修改了Robert Smith的答案,以获取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"
}

必须更改行     -分割为“ n" | ForEach-Object { 至     -split'\ r?\ n'| ForEach-Object {`

和线     如果($ _ -imatch'“ filepath”:“(https://[ ^”] +)“'{ to If ($_ -imatch '"filepath":"(https://[^"]+)"') {

但是在jkd-8u211-windows-x64.exe文件中,我看到了

<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%2fo0U7cEPFSrq1qOmE6B7yKkB3z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FKWIA%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>

不确定下一步该怎么做。 还添加了一些调试爵士乐,例如     #echo“ $ i:$ _” >> Oracle.html ...

答案 5 :(得分:-1)

我在这里得到了这个,所有的功劳归于他...... http://dille.name/blog/2016/06/21/running-minecraft-in-a-windows-container-using-docker/

https://github.com/nicholasdille/docker

它可以工作并下载Java文件。

$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
}