#For this script to run again on a computer, just change the version to a higher number
#The version in the "detection method" in SCCM will also need to be changed.

$PSVersionCompatibability = 3

if($PSVersionTable.PSVersion.Major -ge $PSVersionCompatibability){

#Software name
$softwareName = "Java"

#Software version
$version = "1.8.0.65"

#Log file directory
$logFile = "$env:PUBLIC\Logs\$($softwareName) - $($version).log"

#SCCM Application catalog
$applicationCatalog = "\\venture\svg\SVG-IT\CM-SoftLib\CM-Applications"

#Creator of the install package
$creator = "arivarton"

#Path to installation file or installation executer
$filePath = "msiexec.exe"

#Arguments for install file
$arguments = "/i","jre1.8.0_65.msi","/qn","/norestart","ALLUSERS=1"

#Path to the registry key
$keyPath = "HKLM:\Software\$creator\$softwareName\"

#Copy paths for the configuration file
$copyFromPath = ".\"
$copyToPath = "$env:ProgramData\Oracle\Java\"

#Name of configuration file
$confFile = "java.settings.cfg"

#Check if the registry key is present
$keyCheck = Get-Item $keyPath 2> $null

#Succesfull exit codes
$succesfullExitCodes = @("0","1707")

#If key for software is not present in registry
if(!$keyCheck){
#Create registry key
New-Item -Force $keyPath -ItemType Key > $null

#Make current version the same version as is being installed
$currentVersion = $version
}else{
#Get version from registry
$currentVersion = Get-ItemProperty -Path $keyPath -Name Version

#Last exit code
$scriptLastExitCode = Get-ItemProperty -Path $keyPath -Name "Last Exit Code"
}

#Create registry values
New-ItemProperty -Force -Path $keyPath -Name "Version" -Value $version > $null

#Prepare log file
if(!(Test-Path $logFile)){
New-Item -ItemType file -Path $logFile -Force > $null
}
Write-Output "---------" >> $logFile
Get-Date -Format o >> $logFile

#Stopping all processes that are connected to Java prevents reboot
#Uninstall all Java versions (Credit to cuciu on this post: http://www.itninja.com/question/silent-uninstall-java-all-versions)
Stop-Process -Force -Name iexplorer 2> $null
Stop-Process -Force -Name iexplore 2> $null
Stop-Process -Force -Name firefox 2> $null
Stop-Process -Force -Name chrome 2> $null
Stop-Process -Force -Name jusched 2> $null
Stop-Process -Force -Name jp2launcher 2> $null
Stop-Process -Force -Name java 2> $null
Stop-Process -Force -Name javaw 2> $null
Stop-Process -Force -Name jqs 2> $null
$product = Get-WmiObject win32_product | Where-Object {($_.Name -like "Java*" -and $_.Vendor -like "Oracle*")}
$product.uninstall() > $null

#If version on system is older than this one, install software
if($currentVersion.Version -lt $version -or $succesfullExitCodes -notcontains $scriptLastExitCode.'Last Exit Code'){
#Set registry key version
Set-ItemProperty -Force -Path $keyPath -Name "Version" -Value $version

#Copy configuration file
Robocopy /MIR $copyFromPath $copyToPath $confFile > $null

#Install software
$process = Start-Process -FilePath $filePath -ArgumentList $arguments -Wait -PassThru

New-ItemProperty -Force -PropertyType String -Path $keyPath -Name "Last Exit Code" -Value "$($process.ExitCode)" > $null

#Check exit code and output proper response
if($succesfullExitCodes -contains $process.ExitCode){
Write-Output "$softwareName $version was succesfully installed." | Tee-Object $logFile -Append
exit $process.ExitCode
}
elseif($process.ExitCode -eq "3010"){
Write-Output "$softwareName $version was succesfully installed. Waiting for restart." | Tee-Object $logFile -Append
exit $process.ExitCode
}
else{
Write-Output "Installation of $softwareName $version failed. Exit code: $($process.ExitCode)" | Tee-Object $logFile -Append
exit $process.ExitCode
}
}
#The software has been installed before
else{
Write-Output "Installation of $softwareName $version is already completed. Last installation exit code: $($scriptLastExitCode.'Last Exit Code')" | Tee-Object $logFile -Append
exit 0
}
}

#If Powershell version is not compatible, upgrade it
else{
$logFile = "$env:PUBLIC\Logs\ScriptRequirements.log"

#Prepare log file
if(!(Test-Path $logFile)){
New-Item -ItemType file -Path $logFile -Force > $null
}
Write-Output "---------" >> $logFile
Get-Date -Format o >> $logFile

Write-Output "Powershell version is not up to date. Version $($PSVersionTable.PSVersion.Major) is installed, for this script to run it needs atleast version $PSVersionCompatibability. Management Framework 3.0 is being installed." | Tee-Object $logFile

# Management Framework 3.0 requires .NET Framework 4.0 (Not in the script as this should be installed on all ####### computers

#Install Management Framework 3.0
$process = Start-Process -FilePath wusa.exe -ArgumentList "`"$applicationCatalog\Microsoft\Management Framework\3.0\Windows6.1-KB2506143-x64.msu`" /quiet /norestart" -Wait -PassThru
if($process.ExitCode -eq "0"){
Write-Output "Management Framework 3.0 was succesfully installed." | Tee-Object $logFile
exit $process.ExitCode
}
elseif($process.ExitCode -eq "3010"){
Write-Output "Management Framework 3.0 was succesfully installed. Waiting for restart." | Tee-Object $logFile
exit $process.ExitCode
}
else{
Write-Output "Installation of Management Framework 3.0 failed. Check if the computer has .NET Framework 4.0 or higher installed." | Tee-Object $logFile
exit $process.ExitCode
}
}

To make it run through SCCM

It needs two files:

java.settings.cfg

jre1.8.0_65.msi

These files should be in the same folder as the script.

My Java config settings look like this:

INSTALL_SILENT=Enable
STATIC=Disable
AUTO_UPDATE=Disable
WEB_JAVA=Enable
WEB_JAVA_SECURITY_LEVEL=H
WEB_ANALYTICS=Disable
EULA=Disable
REBOOT=Disable
NOSTARTMENU=Enable
SPONSORS=Disable

(Settings are taken from a ITNinja member)

The Java msi file can be extracted from the exe file.

SCCM Deployment type settings

The deployment type installation program looks like this:

Powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File .\Install.ps1

And the detection method:


The version is added to the registry by the script. This makes it easy to monitor which version is installed.

That is all. Feedback would be appreciated.

// Updated script 26.10.2015

New script for Java version 1.8.0.66:


$PSVersionCompatibability = 3

if($PSVersionTable.PSVersion.Major -ge $PSVersionCompatibability){

#Software name
$softwareName = "Java"

#Software version
$version = "1.8.0.66"

#Log file directory
$logFile = "$env:PUBLIC\Logs\$($softwareName) - $($version).log"

#Creator of the install package
$creator = "*****"

#Path to installation file or installation executer
$filePath = "msiexec.exe"

#Arguments for install file
$arguments = "/i","jre1.8.0_66.msi","/qn","/norestart","ALLUSERS=1"

#Path to the registry key
$keyPath = "HKLM:\Software\$creator\$softwareName\"

#Copy paths for the configuration file
$copyFromPath = ".\"
$copyToPath = "$env:ProgramData\Oracle\Java\"

#Name of configuration file
$confFile = "java.settings.cfg"

#Check if the registry key is present
$keyCheck = Get-Item $keyPath 2> $null

#Succesfull exit codes
$succesfullExitCodes = @("0","1707","3010")

#If key for software is not present in registry
if(!$keyCheck){
#Create registry key
New-Item -Force $keyPath -ItemType Key > $null

#Make current version the same version as is being installed
$currentVersion = $version
}else{
#Get version from registry
$currentVersion = Get-ItemProperty -Path $keyPath -Name Version

#Force install registry key
$forceReinstall = Get-ItemProperty -Path $keyPath -Name ForceReinstall

#Last exit code
$scriptLastExitCode = Get-ItemProperty -Path $keyPath -Name "Last Exit Code"
}

#Create registry values
New-ItemProperty -Force -Path $keyPath -Name "Version" -Value $version > $null

#Prepare log file
if(!(Test-Path $logFile)){
New-Item -ItemType file -Path $logFile -Force > $null
}
Write-Output "---------" >> $logFile
Get-Date -Format o >> $logFile

#Stopping all processes that are connected to Java prevents reboot
#Uninstall all Java versions (Credit to cuciu on this post: http://www.itninja.com/question/silent-uninstall-java-all-versions)
Stop-Process -Force -Name iexplorer 2> $null
Stop-Process -Force -Name iexplore 2> $null
Stop-Process -Force -Name firefox 2> $null
Stop-Process -Force -Name chrome 2> $null
Stop-Process -Force -Name jusched 2> $null
Stop-Process -Force -Name jp2launcher 2> $null
Stop-Process -Force -Name java 2> $null
Stop-Process -Force -Name javaw 2> $null
Stop-Process -Force -Name jqs 2> $null
$product = Get-WmiObject win32_product | Where-Object {($_.Name -like "Java*" -and $_.Vendor -like "Oracle*")}
$product.uninstall() > $null

#If version on system is older than this one, install software
if($currentVersion.Version -lt $version -or $succesfullExitCodes -notcontains $scriptLastExitCode.'Last Exit Code' -or $forceReinstall.ForceReinstall -eq 1){

#Output if the installation is being forced
if($forceReinstall.ForceReinstall -eq 1){
Write-Output "$(Get-Date -UFormat '%H:%M:%S'): Forcing reinstallation of $softwareName $version." | Tee-Object $logFile -Append
}

#Set registry key version and force installation to 0
Set-ItemProperty -Force -Path $keyPath -Name "Version" -Value $version
New-ItemProperty -Force -PropertyType String -Path $keyPath -Name "ForceReinstall" -Value "0" > $null

#Copy configuration file
Robocopy /MIR $copyFromPath $copyToPath $confFile > $null

#Install software
$process = Start-Process -FilePath $filePath -ArgumentList $arguments -Wait -PassThru

New-ItemProperty -Force -PropertyType String -Path $keyPath -Name "Last Exit Code" -Value "$($process.ExitCode)" > $null

#Check exit code and output proper response
if($succesfullExitCodes -contains $process.ExitCode){
Write-Output "$(Get-Date -UFormat '%H:%M:%S'): $softwareName $version was succesfully installed." | Tee-Object $logFile -Append
exit $process.ExitCode
}
elseif($process.ExitCode -eq "3010"){
Write-Output "$(Get-Date -UFormat '%H:%M:%S'): $softwareName $version was succesfully installed. Waiting for restart." | Tee-Object $logFile -Append
exit $process.ExitCode
}
else{
Write-Output "$(Get-Date -UFormat '%H:%M:%S'): Installation of $softwareName $version failed. Exit code: $($process.ExitCode)" | Tee-Object $logFile -Append
exit $process.ExitCode
}
}
#If uninstall key is set
elseif($args[0] -eq "uninstall"){
Write-Output "$(Get-Date -UFormat '%H:%M:%S'): Uninstall key was set. Removing $softwareName and $creator registry items for $softwareName." | Tee-Object $logFile -Append
$product = Get-WmiObject win32_product | Where-Object {($_.Name -like "Java*" -and $_.Vendor -like "Oracle*")}
$product.uninstall() > $null
Remove-Item $keyPath -Force -Recurse 2> $null
}
#The software has been installed before
else{
Write-Output "Installation of $softwareName $version is already completed. Last installation exit code: $($scriptLastExitCode.'Last Exit Code')" | Tee-Object $logFile -Append
exit 0
}
}

#If Powershell version is not compatible, write to console and logs
else{
$logFile = "$env:PUBLIC\Logs\ScriptRequirements.log"

#Prepare log file
if(!(Test-Path $logFile)){
New-Item -ItemType file -Path $logFile -Force > $null
}
Write-Output "---------" >> $logFile
Get-Date -Format o >> $logFile

Write-Output "$(Get-Date -UFormat '%H:%M:%S'): Powershell version is not up to date. Version $($PSVersionTable.PSVersion.Major) is currently installed. For this script to run succesfully it needs atleast version $PSVersionCompatibability. Management Framework 3.0 provides the correct version (KB2506143)." | Tee-Object $logFile
}

This will require three detection methods in SCCM:


&&

&&