Given the overall similarities and program flow of the various CMD and VBS scripts used in package deployment, I decided to combine their purposes into a single PowerShell script that uses a set of options to specify the action to be undertaken. Like the Install.cmd and Update.cmd scripts before this one, this script is designed to be run in a multiple of ways - interactively by clicking on it, via the command line, via MDT, and via SCCM.
This script will quit PowerShell and return an exit code to the calling process (i.e., SCCM or MDT). To call this script, the command line should be:
PowerShell.exe -ExecutionPolicy Bypass -File .\Install.ps1 -Install
When running from within SCCM, if the client policy is to bypass script signing anyway, you can skip the PowerShell.exe -ExecutionPolicy Bypass -File bit and just have the Install.ps1 -Install portion. The first part is, however, required under MDT since the execution policy, by default, requires signing (or is outright disabled).
The script was designed so that regular updates to the application can be made easier by just updating the top portion of the script; everything else is, generally, going to stay the same. When you first create the script for a particular application, there are functions that you need to update (denoted by !!_UPDATE_THIS_!! tags).
However, the top of the script just has some very simple variables that end up getting used. For this particular example, we'll be repackaging the Java Runtime Environment. To do that, we've already obtained the MSI file from the web downloaded installer.
User Defined Application Install Functions
In the following sections, there are three functions that must be defined and are specific to the application being installed. From a practical point of view, once defined, these functions shouldn't need to change version to version as you re-use the script for the application.
- myUninstallOldApplication - This function closes processes and uninstalls the application.
- myInstallNewApplication - This function handles the basic installation of the new application.
- myConfigureApplication - If the installation succeeded, this function will allow you to add additional files or registry settings in order to configure the application to meet your needs
Installation Status and Version
As a part of the script, we check to see if the application is installed and, if so, what version is installed. This script will not attempt a downgrade - only upgrades. If the version that we're installing is already installed, do nothing (no repairs).
To do that, we have a selection of helper functions that are called in the script's main body to get version data from the computer and then evaluate it.
Main Script Body
Now that we've got the functions out of the way, we come to the main script body.