First you need to get the latest Download Link(32-bit).
$JavaFile = [pscustomobject]@{
JavaVersion = ''
FileName = ''
DownloadURL = $((Invoke-WebRequest 'http://www.java.com/en/download/manual.jsp').links | where innerHTML -like "Windows Offline" | select href).href
}
For 64-bit URL use:
$JavaFile = [pscustomobject]@{
JavaVersion = ''
FileName = ''
DownloadURL = $((Invoke-WebRequest 'http://www.java.com/en/download/manual.jsp').links | where innerHTML -like "Windows Offline (64-bit)" | select href).href
}
So now we want to check if we already downloaded the file.
We can do this by checking a log file. So first create a javahistorylog.log for storing used links.
With the following Function we can Check the log for links.
function CheckHistory ($fileurl,$path){
$history=Get-Content "$path"
$r = $false
Foreach ($historicurl in $history){
if ($historicurl -eq $fileurl){
$r = $true
}
}
return $r
}
If it wasn't found we can continue with downloading the File.
$JavaFile.FileName = "tempinstaller$(get-date -Format yyMMddmm).exe"
Invoke-WebRequest $JavaFile.DownloadURL -OutFile ("$TempDownloadLocation\"+ $JavaFile.FileName) -ev $DLErr
if($DLErr){Exit}
After the Download we can do some edits to the setup.exe
First get the Version and safe the name.
$TempFileName = $JavaFile.FileName
$JavaFile.JavaVersion = get-item ("$TempDownloadLocation\"+ $JavaFile.FileName) | select -ExpandProperty versioninfo | select -ExpandProperty productversion
Second set and generate the name out of the originalfilename prop.
$JavaFile.FileName = "jre1."+(((Select-String -Pattern '[0-9]u[0-9]+' -InputObject (get-item ("$TempDownloadLocation\$TempFileName") | select -ExpandProperty versioninfo | select -ExpandProperty originalfilename)) |
ForEach-Object -Process {
$_.Matches
} |
ForEach-Object -Process {
$_.Value
}) -replace 'u', '.0_')
now rename the File.
Rename-Item -Path "$TempDownloadLocation\$TempFileName" -NewName ($JavaFile.FileName+".exe")
update the historylog
if(Test-Path -path ("$TempDownloadLocation\"+$JavaFile.FileName+".exe")){
add-content "$TempDownloadLocation\javahistorylog.log" $JavaFile.DownloadURL
}
Now we can think about getting the MSI File for building a sw package.
The MSI will be located at:
$MsiFilePathTemp = "$env:LOCALAPPDATA\Oracle\Java\" -replace 'Local', 'LocalLow'
We can get the MSI by executing the setup and coping the msi:
Start-Process -FilePath ("$TempDownloadLocation\"+$JavaFile.FileName+".exe") -ArgumentList '/s'
while(!(Test-Path ("$MsiFilePathTemp\"+$JavaFile.FileName+"\"+$JavaFile.FileName+".msi")))
{
Start-Sleep -Seconds 1.5
}
Copy-Item -Path ("$MsiFilePathTemp\"+$JavaFile.FileName+"\"+$JavaFile.FileName+".msi") -Destination $MsiFilePathOut -Force -ErrorVariable $CDR
if($CDR){
exit
}
Now Stop the Process and delete the .exe file
Get-Process -Name $JavaFile.FileName | Stop-Process
While (Get-Process -Name $JavaFile.FileName -ErrorAction SilentlyContinue)
{
Start-Sleep -Seconds 2
}
Remove-Item -Path ("$TempDownloadLocation\"+$JavaFile.FileName+".exe") -Force
$MSIFile = ("$MsiFilePathOut\"+$JavaFile.FileName+".msi")
Now you have $MSIFile
which is the path of the msi you could use for building your SCCM Package.