/build/static/layout/Breadcrumb_cap_w.png

Powershell script for deployment of Java and general applications


#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:


&&

&&



Comments

  • What version of SCCM is the screen shot for? - empyre 5 years ago
  • Hello Arivarton,Java msi can be deployed via SCCM using the simple msi command line arguments, right? Why do you suggest this complicated method? Is there any advantage in using the PS script instead of regular windows batch script? - seemon 4 years ago
    • There are three advantages with this script.
      First thing is that it will uninstall all previous versions of Java on the machine.
      Secondly it copies the .cfg to the local machine.
      Third reason is that it will automatically shutdown services that are associated with Java, this guarantees that the machine won't restart.

      I also like to use it for the custom logging part and the registry addition. The .msi logs can be a bit messy.

      Basically I use this script for every application i deploy with SCCM. This ensures that each client machine has the same installation logs for each software.

      Personally I find that deploying software with powershell scripts makes the packaging of applications go much faster as Powershell has a lot of advantages to batch scripts. It offcourse requires the packager to know a bit of Powershell and basic programming.

      The script has been updated a bit since this version, I will remake this post to reflect that. - arivarton 4 years ago
  • Thank you for the detailed explanation - seemon 4 years ago
  • Arivarton, could you please explain me how to deploy Java using this script via SCCM? I know how to do using the windows batch file. It would be really helpful if you could post the step by step instructions. And, what are the changes that need to be done on this script for deploying other application? - seemon 4 years ago
This post is locked

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ