Tuesday, 2 May 2017

Powershell - Download and install Java using powershell

I got tired of updating the JRE package for our windows task sequences and used powershell to scrape for web-links from https://www.java.com/en/download/manual.jsp and install it silently.


<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.138
Created on: 5/2/2017 12:31 PM
Created by: tausifkhan
Organization: FICO
Filename:
===========================================================================
.DESCRIPTION
download and install java from the java download link.
#>
$PSScriptRoot = ($MyInvocation.MyCommand.Path | Split-Path | Resolve-Path).ProviderPath
#check https://www.java.com/en/download/manual.jsp for windows JRE download links and install
$javax64install = ((Invoke-WebRequest –Uri 'https://www.java.com/en/download/manual.jsp').Links | Where-Object { $_.innerHTML -eq "Windows Offline (64-bit)" }).href
$javax86install = ((Invoke-WebRequest –Uri 'https://www.java.com/en/download/manual.jsp').Links | Where-Object { $_.innerHTML -eq "Windows Offline" }).href
#logging variables.
$logdir = "$($env:ALLUSERSPROFILE)\InstallLogs"
$Logfile = "$($logdir)\install_java_$(get-date -format `"yyyyMMdd_hhmmsstt`").log"
Function LogWrite($string, $color)
{
if ($Color -eq $null) { $color = "white" }
write-host $string -foregroundcolor $color
$string | out-file -Filepath $logfile -append
}
if (!(Test-Path -Path $logdir))
{
New-Item -ItemType directory -Path $logdir
}
$javax64 = "$PSScriptRoot\javaX64.exe"
$javax86 = "$PSScriptRoot\javax86.exe"
if (Get-Command 'Invoke-Webrequest')
{
Invoke-WebRequest $javax64install -OutFile $javax64 -PassThru
Invoke-WebRequest $javax86install -OutFile $javax86 -PassThru
}
else
{
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($javax64install, $javax64)
$WebClient.DownloadFile($javax86install, $javax86)
}
If (Test-Path $javax64) {
$javax64install = Start-Process -FilePath $javax64 -ArgumentList "/s INSTALL_SILENT=1 STATIC=0 AUTO_UPDATE=0 WEB_JAVA=1 WEB_JAVA_SECURITY_LEVEL=H WEB_ANALYTICS=0 EULA=0 REBOOT=0 NOSTARTMENU=0 SPONSORS=0 /L $Logfile" -Wait -Verbose -PassThru
Start-Sleep -s 35
if ($javax64install.ExitCode -eq 0) {
LogWrite "Successfully Installed Java RE X64, removing installer" -color yellow
remove-item $javax64 -Force
}
else {
Logwrite "Java 64 bit installer exited with exit code $($javax64install.ExitCode)" -color red
}
}
else {
LogWrite "Cannot find $javax64" -color red
}
If (Test-Path $javax86) {
$javax86install = Start-Process -FilePath $javax86 -ArgumentList "/s INSTALL_SILENT=1 STATIC=0 AUTO_UPDATE=0 WEB_JAVA=1 WEB_JAVA_SECURITY_LEVEL=H WEB_ANALYTICS=0 EULA=0 REBOOT=0 NOSTARTMENU=0 SPONSORS=0 /L $Logfile" -Wait -Verbose -PassThru
Start-Sleep -s 35
if ($javax86install.ExitCode -eq 0) {
LogWrite "Successfully Installed Java RE X86, removing installer" -color yellow
remove-item $javax86 -Force
}
else {
Logwrite "Java 32 bit installer exited with exit code $($javax86install.ExitCode)" -color red
}
}
else {
LogWrite "Cannot find $javax86" -color red
}
view raw jre-install.ps1 hosted with ❤ by GitHub

No comments:

Post a Comment

Detect autopilot session

  Ensuring that some apps only install during autopilot is not easily accomplished, you can use the below powershell script as a requiremen...