Friday, May 14, 2010

Enumerate the Windows Uninstall key

Recently I created an application install script where I needed to scan for the existence of another MSI based install and remove it (if found) before installing a newer version. This script uses the StdRegProv object to create an array of subkeys in SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall.

============================
Option Explicit

Const blnDebug = False
Const HKEY_LOCAL_MACHINE = &H80000002

Dim sSystemFolder, sUserProfile, sQuickLaunch, sComputer, oReg
Dim strKeyPath, subkey, arrSubKeys
Dim oWSH : Set oWSH = CreateObject("WScript.Shell")
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oNET : Set oNET = CreateObject("WScript.Network")
sUserProfile = oWSH.ExpandEnvironmentStrings("%USERPROFILE%")
sQuickLaunch = sUserProfile & "\Application Data\Microsoft\Internet Explorer\Quick Launch"
sSystemFolder = oFSO.GetSpecialFolder(1)
sComputer = oNET.ComputerName

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys
If subkey = "{ProductGUID#1}" Or subkey = "{ProductGUID#2}" Then
'WScript.Echo subkey & " Application found, removing it now..."
oWSH.Run "MSIEXEC.EXE /X" & subkey & " /qn", 1, False
End If
Next

InstallMSIMST "\\servername\Packages\appname\appversion", "Setup.msi", "\\servername\Packages\appname\appversion", "Setup.mst", " /qn /norestart"

Function InstallMSIMST(szMSIPath, szMSIFile, szMSTPath, szMSTFile, szParams)
oWSH.Run (sSystemFolder & "\msiexec.exe /i " & Chr(34) & szMSIPath & "\" & szMSIFile & Chr(34) & " TRANSFORMS=" & Chr(34) & szMSTPath & "\" & szMSTFile & Chr(34) & szParams), 0, True
End Function

============================

Mitch

1 comment:

JJ said...

Love your work mate!!