[download id=”60″]

a couple of weeks ago I wrote an article about the new script that enabled the user to find product code automatically from the product have, you can read it here

I promised to do a follow up! explaining how to uninstall, then afterward install iTunes (or any other product), without knowing the version/product code.

By using the function from the other article this is very easily done:

' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution:  iTunes
' // File:      Upgrade and Install.vbs
' // Author:	Jakob Gottlieb Svendsen, Coretech A/S. https://blog.ctglobalservices.com/jgs
' // Purpose:   Uninstall old versions and install new.
' //			Searches registry for the productcodes for the old programs.
' //
' // Usage:     Upgrade and Install.vbs
' //
' //
' // CORETECH A/S History:
' // 1.0.0     JGS 18/10/2010  Created initial version.
' //
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************
'//----------------------------------------------------------------------------
'//
'//  Global constant and variable declarations
'//
'//----------------------------------------------------------------------------

Option Explicit

Dim ObjShell, ReturnVal, ExistingVersion, ProductCode, objWMIService, objProcess, colProcess, ProcessesFound, ProductName

'//----------------------------------------------------------------------------
'//  Main routines
'//----------------------------------------------------------------------------

Set ObjShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2") 

Uninstall GetProductCode("itunes"), "Apple iTunes"
Uninstall GetProductCode("bonjour"), "Apple Quicktime"
Uninstall GetProductCode("apple mobile"), "Apple Bonjour"
Uninstall GetProductCode("apple application"), "Apple Application Support"
Uninstall GetProductCode("apple software update"), "Apple Software Update"

	'Install new version
	ReturnVal = ObjShell.run ("msiexec /I ""Bonjour.msi"" TRANSFORMS=""Bonjour.mst"" /QN reboot=reallysuppress", 0, True)
	If Not (ReturnVal = 0 Or ReturnVal = 3010) then LogError("Bonjour Install Failed! ReturnCode: " & ReturnVal)

	ReturnVal = ObjShell.run ("msiexec /I ""AppleApplicationSupport.msi"" /Passive /QN reboot=reallysuppress", 0, True)
	If Not (ReturnVal = 0 Or ReturnVal = 3010) then LogError("Apple Application Install Failed! ReturnCode:" & ReturnVal)

	ReturnVal = ObjShell.run ("msiexec /I ""iTunes.msi"" /QN reboot=reallysuppress ALLUSERS=2 IAcceptLicense=""Yes"" IS_ASU=1 REGSRCH_DESKTOP_SHORTCUTS=0 ROOTDRIVE=""C:\"" SCHEDULE_ASUW=0", 0, True)
	If Not (ReturnVal = 0 Or ReturnVal = 3010) then LogError("Apple iTunes Install Failed! ReturnCode:" & ReturnVal)	

	ReturnVal = ObjShell.run ("msiexec /I ""AppleMobileDeviceSupport.msi"" /QN reboot=reallysuppress", 0, True)
	If Not (ReturnVal = 0 Or ReturnVal = 3010) then LogError("Apple Mobile Device Support! ReturnCode:" & ReturnVal)

Set ObjShell = Nothing

'//----------------------------------------------------------------------------
'//  Procedures
'//----------------------------------------------------------------------------

Function Uninstall(ProductCode, ProductName)
	On Error Resume Next
	Set ObjShell = CreateObject("WScript.Shell")

	If ProductCode <> "" Then
		ExistingVersion = 0
		ExistingVersion = ObjShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & ProductCode & "\")

		ObjShell.LogEvent 4, "Seaching for installed product: " & ProductName & " - " & ProductCode

		If ExistingVersion <> "" Then
			ObjShell.LogEvent 4, "Found " & ProductName & " - " & ProductCode
			ReturnVal = ObjShell.run ("Msiexec.exe /x " & ProductCode & " /QN reboot=reallysuppress", 1, True)
			If Not (ReturnVal = 0 Or ReturnVal = 3010) then LogError("Could not Uninstall " & ProductName & "!")
		End If

		ExistingVersion = "0"
	Else
			ObjShell.LogEvent 4, "No product code supplied for product: " & ProductName & " - " & ProductCode & ", no search will be done"
	End If
End Function

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

Function LogError (Message)
	ObjShell.LogEvent 1, "Installation Error: " & Message & " MSI Return Code: " & ReturnVal
   WScript.Quit ReturnVal
End Function

'//----------------------------------------------------------------------------
'//  End Script
'//----------------------------------------------------------------------------

All we have is the product code finder function, and uninstall function, and a log function.

We call the uninstall function sending the productcode as a parameter.

This product code I found by using the GetProductCode function in the same line.

After the uninstall, we install the msi packages again. Using error handling we check if the install is OK (0 = Success, 3010 = success but reboot pending)

otherwise we call the logerror function, which will log the error to the errorlog on the local PC. this means we can after a deployment, connect to the eventlog and read the error messages, if needed.