Wednesday, January 12, 2011

Install application based on OS version by using a Batch File

One of the things that I need to do from time to time is install an application depending on the OS type and version. For example, I need to install Bluetooth software on a Fujitsu laptop when the OS is Windows XP but not Windows 7. I need to run the install script to deploy the software across the network. Also, I need to make sure the install script will run on Windows XP, Vista, and Windows 7.

Because of the variety of applications that I must have in my environment in addition to the variety of desktop OSes, I choose to use batch files. I could have used VBScript, but for me batch files are easier to work with and easier to change on the fly. Plus the command line window that displays onscreen at least lets me know that there is something happening on the computer. What I needed to do was find out how to customize the install script to find out what version of Windows installed then only execute the installation process if the version is Windows XP. If the version of Windows installed didn’t match, then go to the end of the batch file where it terminates via EXIT.

To my surprise, this was quite easy to do. Using ERRORLEVEL and Labels in the batch file helped resolve my dilemma.

Example:

@ECHO OFF
Ver | Find "XP" > Nul
If not ErrorLevel 1 Echo OS is Windows XP
GOTO INSTALL

Ver | Find "7600" > Nul
If not ErrorLevel 1 Echo OS is Windows 7
GOTO GOODBYE

:INSTALL
\\server\distribution_share\Application\Setup.msi /passive /quiet
EXIT

:GOODBYE
EXIT

First, we call VER which retrieves the version of Windows installed on the computer. If the version has “XP” in the string, then go to the label “INSTALL” where the actual script work is located. If the version has “7600” in the string then go to the label “GOODBYE” where the batch file terminates. This works great in Windows XP, Vista, and Windows 7 computers.

The script can be edited to do the opposite. Install the application if it is Windows 7 and not Windows XP, and so on. This script can also be edited so that it executes a command, or set of commands, instead of executing a software install.

Of course, there may be a better way to go about scripting the install, such as Powershell, but this is my own solution. Simple and easy.