There are times in a script that you may need to know if a specific patch is installed on a PC. You might be deploying a newer patch that depends on a previous patch being present. You might be deploying a new application that requires another product to be patched to a certain level. For patches deployed in Microsoft’s "MSP" format, the Windows Installer object will allow you to determine if the patch is installed.
The sample code below will, for the product code specified, provide a list of all applied patches. (The product code can be found inside the MSI which installs the product. It can also be found in the registry under "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" and/or "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" on a 64-bit system. Apps installed by non-administrator users through Active Directory published apps may be found in a different location.) See the screenshot below to learn how to identify a product code in Regedit:
With the application’s product code, it is possible to identify any installed patches. The following sample code will display the patches currently installed for Microsoft Office 2010 Pro Plus (based on its product code of ""):
installedPatches = GetInstalledPatches("{91140000-0011-0000-0000-0000000FF1CE}")
for each thisPatch in installedPatches
wscript.echo thispatch
next
wscript.quit
Function GetInstalledPatches(strProductGuid)
Dim oMSI, patchList, counter, thisPatch
Dim resultList()
Set oMSI = CreateObject("WindowsInstaller.Installer")
Set patchList = oMSI.Patches(strProductGuid)
ReDim resultList(patchList.Count – 1)
counter = 0
For Each thisPatch In patchList
resultList(counter)=CStr(thisPatch)
counter = counter+1
Next
GetInstalledPatches = resultList
Set patchList = Nothing
Set oMSI = Nothing
End Function
Provided with the product code and patch GUID, the following function will return a true response if the specified patch is installed, or false if it isn’t:
‘
‘ Given a product Guid and patch Guid, this function returns
‘ a True if the patch is installed, or False if it isn’t.
‘
Function PatchIsInstalled(strProductGuid,strPatchGuid)
Dim oMSI, patchList, thisPatch
Set oMSI = CreateObject("WindowsInstaller.Installer")
Set patchlist = oMSI.Patches(strProductGuid)
For Each thisPatch In patchlist
If thisPatch = strPatchGuid Then
PatchInstalled = True
Exit Function
End if
Next
‘ If we got to here, the patch is not installed.
PatchInstalled = False
Set patchList = Nothing
Set oMSI = Nothing
End function