VBScript: Uninstall and Install iTunes (Upgrade) using automatic product code finder script (or any other product, by using product name)

zip Upgrade_and_Install_blog.coretech.dk.zip 1.84 kB
Downloaded 504 times

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. http://blog.coretech.dk/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.


Comments (9):

  1. vern says:

    Thanks very much for the code – was looking for something like this…one question though

    The code:
    ObjShell.LogEvent 4, “Found Installed “& strName &” product Code: ” & Replace(Replace(strValue, “MsiExec.exe /X”,””), “MsiExec.exe /I”,””)
    115.
    If strValue “” Then
    116.
    GetProductCode = Replace(Replace(strValue, “MsiExec.exe /X”,””), “MsiExec.exe /I”,””)

    is not returning the full silent uninstall, so then nothing uninstalls. ie msiexec /x {xxxxxxxxx…], I’ve been looking in the event viewer logs and also changed the code to write out to a logfile to make sure. It finds my product but does not uninstall (in my case I’m trying to uninstall adobe reader 9 for a test.

    thanks,

  2. Steve says:

    I’m trying to adapt this script to uninstall Live Meeting and it does not uninstall anything.

  3. Continúa says:

    Continúa

    Coretech Blog » Blog Archive » VBScript: Uninstall and Install iTunes (Upgrade) using automatic product code finder script (or any other product, by using product name)

  4. Hey! This is kind of off topic but I need some help from an established blog.

    Is it very difficult to set up your own blog? I’m not very techincal
    but I can figure things out pretty fast. I’m thinking about setting
    up my own but I’m not sure where to begin. Do you have any points or suggestions?
    Appreciate it

    my website :: landscaping virginia beach

  5. Stefan says:

    We deploy iTunes to a lot of peobles and this tool will make it easier. There is a good special script, but does not uninstall my old iTunes-installations. in the Event-viewer find correct string, but does not uninstall. Can you help us? Thanks

  6. […] VBScript : Uninstall and Install iTunes (Upgrade …Home » Jakob Gottlieb Svendsen » VBScript: Uninstall and Install iTunes … “Apple Application Support” Uninstall GetProductCode … Uninstall and Install iTunes …Kan du besøge hjemmesiden […]

  7. We’re a group of volunteers and starting a new scheme in our community.
    Your site offered us with valuable information to work on. You’ve done an impressive job and our
    whole community will be thankful to you.

  8. Ayesha says:

    Hello,

    I want to uninstall Itunes from multiple computers silently. how can I call this procedure for uninstallation.

    Regards,

Leave a Reply