Utopia
Utopia

Reputation: 661

Script to check the system for already installed program and its version

I came upon a task of writing a script which first initiates a silent installation of Websphere MQ client 32 bit on a windows server. Then check if the installation was successful or not..... So I wrote the following script:

  @echo off
  REM Author : Akshay Sinha
  REM Date Created : 07/05/2012
  REM Installing Websphere MQ.......
  Msiexec /q /i "%CD%\MSI\IBM WebSphere MQ.msi" /l*v .\install.log /m mif_file             TRANSFORMS="1033.mst" AGREETOLICENSE="yes"
  echo Script to check if the installation failed !!!
  echo Waiting for installaion to complete.......
  REM Script will wait for 2 mins, This is to ensure that install.log gets fully             generated.
  ping 123.45.67.89 -n 1 -w 120000 > nul
  echo Wait Over
  find /C "Installation operation failed" "%CD%"\install.log > tmp.log
  for /f "tokens=1,2,3 delims=:" %%a in (tmp.log) DO (
  SET /a FOUND_STR=%%c
  echo %FOUND_STR%
  )
  del tmp.log
  SET %FOUND_STR%=%FOUND_STR: =%
  echo %FOUND_STR%
  if %FOUND_STR% EQU 0 (
  echo Installation Of MQ completed without any errors!!!!!
  EXIT /B 0
  )
  if %FOUND_STR% GTR 0 (
  echo There were errors while installing MQ.. Pls Verify!!!
  EXIT /B 1
  )

the script is working fine for a fresh installation. ie if the mentioned software was not installed on the system already.

However, I need to enhance this script such that, it should check the system for existing installations of Websphere MQ and its version. --if the version is not the one which we require(which of course , i will provide from command line) , it should initiate a uninstallation.

The problem is I don't want to use the approach of searching the file system. So how do I accomplish this task using WMI classes.?? I looked up Win32_Product class, but it only returned me one installed program(Although, I have arnd 40 applications installed on my system).So I want to know: 1)what is the concrete method of searching for a particular program on a system.(I am open to VbScripting or batch programming) 2)Does the value of registry key for a installed software remains same across all systems and does it differ with different virsions??

Thanks in advance.

Upvotes: 4

Views: 11669

Answers (2)

Utopia
Utopia

Reputation: 661

Thanks PA for that TIP.. however I came up with a solution using VBScript. I am calling this script from my original batch file. Actually I wrote a vb script which enumerates the different registry keys available at :

      SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\

and search for "IBM WebSphere MQ" as the "DisplayName". When found , it will then list its version number and "Product code". Please note that we can use "Product code" for installation purpose from commandline... Following is the script I wrote. I am posting it here for everyone's use. Please feel free to use it..........

  '------------------------------------------------------------------------------------      
  'Script Name : listMQ.vbs
  'Author      : Akshay Sinha
  'Created     : 05/10/12
  'Description : This Script attempts to check if the correct version of Websphere MQ             is already installed on the system.
  '            : If found the Script will exit with a ERRORLEVEL of 0.
  '            : If found but not of correct version... Script will exit with a 
  '              ERRORLEVEL of 1..Which in turn will initiate a Uninstalation      
  '            : If not found, Script will exit with a ERRORLEVEL of 2 and initiate a 
  '              a fresh installation.
  '            : Syntax: at command prompt ->
  '            : C:>Cscript listMQ.vbs
  '            : Check the value of %ERRORLEVEL% after execution. 
  '            : C:>echo %ERRORLEVEL%. Should give 0,1 or 2
  '------------------------------------------------------------------------------------

    Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE

    strComputer = "."
    strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    strEntry1a = "DisplayName"
    strEntry1b = "QuietDisplayName"
    strEntry1c = "DisplayVersion"
    strEntry1d = "UninstallString"

    Set objReg = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv")
    objReg.EnumKey HKLM, strKey, arrSubkeys
    WScript.Echo "Installed Applications" & VbCrLf
intVersionNum="1.0.0.0"
    For Each strSubkey In arrSubkeys
    intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, strEntry1a, strValue1)
if intRet1 <> "" Then
objReg.GetExpandedStringValue HKLM, strKey & strSubkey, strEntry1a, strValue1
intCompare=StrComp("IBM WebSphere MQ",strValue1,vbTextCompare)
    IF intCompare = 0 THEN
objReg.GetExpandedStringValue HKLM, strKey & strSubkey, strEntry1c, intVersionNum
strUninstall=strSubkey
WScript.Echo "Congratulations!! Websphere MQ is already installed on this system"
        WScript.Echo strEntry1a & " : " & strValue1 
        WScript.Echo strEntry1c & " : " & intVersionNum 
    END IF
End If
Next

IF intVersionNum="1.0.0.0" THEN
        WScript.Echo "Sorry Websphere MQ was not found on this system" 
        WScript.Quit 2
END IF

intVersionCompare=StrComp("7.0.1.5",intVersionNum,vbTextCompare)
IF intVersionCompare = 0  THEN
WScript.Echo "Congratulations!! Correct version of Websphere MQ is installed" 
WScript.Echo "Uninstall String for this product is : " & strUninstall
WScript.Quit 0
ELSE
WScript.Echo "Wrong Version of MQ installed"
WScript.Echo "Initiating Unistallation....."
WScript.Quit 1
END IF

This is a pretty self explanatory script..Please let me know if you run into any issues.

Upvotes: 1

PA.
PA.

Reputation: 29339

you don't probably need WMI to achieve this. Just query the registry for the hklm\software\Microsoft\Windows\CurrentVersion\Uninstall\ key, and check the returned values.

begin with something along this code...

for /f "tokens=*" %%a in ('reg query hklm\software\Microsoft\Windows\CurrentVersion\Uninstall\ ^| find /i "IBM Websphere MQ" ') do (
 echo %%a
)

Upvotes: 1

Related Questions