[download id=”58″]

Today I am working on a installation package for iTunes 10.5.

We have to deploy it via Configuration Manager 2007 R2.

Beforehand we used to manually insert all product codes in the uninstall/upgrade script, to make sure that all version of itunes and support programs was uninstalled.

This was because we have experienced problems when upgrading, without removing old version first.

Now I have created a function that automatically can search both 32bit and 64bit registry for a specific product, and return the product code which can be used for automatic uninstall.

Here is the script:

Function GetProductCode(strName)

	Dim strComputer, oReg, strKeyPath, strValueNAme, strValue, arrSubKeys, subkey

	Const HKEY_LOCAL_MACHINE = &H80000002
	strComputer = "."
	arrSubKeys = Null

	Set ObjShell = CreateObject("WScript.Shell")

	ObjShell.LogEvent 4, "Seaching in registry for installed products by search term: " & strName 

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

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

	If Not IsNull(arrSubKeys) Then
		For Each subkey In arrSubKeys
		   ' WScript.Echo subkey
		    strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & subkey
			strValueName = "DisplayName"
			oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

			If InStr(LCase(strValue), LCase(strName)) > 0 Then

				strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & subkey
				strValueName = "UninstallString"
				oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
				ObjShell.LogEvent 4, "Found Installed "& strName &" product Code: " & Replace(Replace(strValue, "MsiExec.exe /X",""), "MsiExec.exe /I","")
				If strValue <> "" Then
					GetProductCode = Replace(Replace(strValue, "MsiExec.exe /X",""), "MsiExec.exe /I","")
				End If
			End If
		Next
	End If
	arrSubKeys = Null

	ObjShell.LogEvent 4, "Seaching in Wow6432Node registry for installed products by search term: " & strName 

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

	If Not IsNull(arrSubKeys) Then

	For Each subkey In arrSubKeys
	   ' WScript.Echo subkey
	    strKeyPath = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & subkey
		strValueName = "DisplayName"
		oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

		If InStr(LCase(strValue), LCase(strName)) > 0 Then

			strKeyPath = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" & subkey
			strValueName = "UninstallString"
			oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
			ObjShell.LogEvent 4, "Found Installed "& strName &" product Code: " & Replace(Replace(strValue, "MsiExec.exe /X",""), "MsiExec.exe /I","")
	 		If strValue <> "" Then
	 			GetProductCode = Replace(Replace(strValue, "MsiExec.exe /X",""), "MsiExec.exe /I","")
	 		End If
		End If
	Next
End If
End Function

use the function as the following:

WScript.Echo GetProductCode("itunes")
WScript.Echo GetProductCode("bonjour")
WScript.Echo GetProductCode("apple mobile")
WScript.Echo GetProductCode("apple application")
WScript.Echo GetProductCode("apple software update")

Download the script above for the full implementation

Next article decribes how to use it in a Uninstall/Install  aka. upgrade scenario. Find it here

Happy Scripting!