Thursday, October 6, 2016

PowerShell: Download and Install Java 8 JRE

This script I wrote for our automation system that will:
  1. Download the latest Java 8 JRE from Java.com
  2. Save in a local directory C:\Install
  3. Create a Install Options File
  4. Run the installer silently with the necessary Installer Options
The Installer Options file determines what configuration is used the install of Java.

For example, in the script I have configured. This will perform a silent install, auto updates enabled, no reboot after the installation, Sponsors/ads install disabled and will uninstall any older existing installations of Java JRE.

  • INSTALL_SILENT=Enable
  • AUTO_UPDATE=Enable
  • REBOOT=Disable
  • SPONSORS=Disable
  • REMOVEOUTOFDATEJRES=Enable
You can find these and other options to use from the Java website:


This has been tested to work on PowerShell v2 and higher.


--------------------------------------------------------------------------
                Deploy Java JRE Software
                  Date: 21-Sept-2016
               Created by: Daniel Burrowes
--------------------------------------------------------------------------

Description:
Downloads the Java installer and installs silently

-------------------------------------------------------------------------- 

# -Verbose and -Debug

    [CmdletBinding()]
    param(    )

# --------------------------------------------------------------------------

Write-Verbose "Setting Global Variables..."
$InstallDir = "c:\Install\Java"
$Source = "http://javadl.oracle.com/webapps/download/AutoDL?BundleId=211996"
$Destination = "$InstallDir\java.exe"
$Options = "$InstallDir\java_options.txt"


#Create install directory
Write-Verbose "Creating Install Directory"
New-Item -Path $InstallDir -ItemType directory -Force

Write-Verbose "Downloading Software..."
$start_time = Get-Date
(New-Object System.Net.WebClient).DownloadFile($Source, $Destination)
Write-Verbose "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)" 

Write-Verbose "Creating Installer Options File..."
$text = 'INSTALL_SILENT=Enable
AUTO_UPDATE=Enable
REBOOT=Disable
SPONSORS=Disable
REMOVEOUTOFDATEJRES=Enable
'

# Create file
$text | Set-Content $Options

#Running the installer
Write-Verbose "Executing Java Install.."
Start-Process -FilePath $Destination -ArgumentList "INSTALLCFG=$Options /s /L $InstallDir\jre-install.log" -Wait -Verbose -PassThru