/build/static/layout/Breadcrumb_cap_w.png

Q&D method of getting an MSI's ProductCode

I know I'm supposed to have retired from ITN but well, I thought I'd share one last thing.

When you're writing scripts or adding MSIs to SCCM or whatever, what a monumental pain it is to have to load the MSI into Orca/InstEdit, scroll to the Properties table then scroll to the ProductCode property, eh? Well, no more! Here's a script which will echo that property to the screen and copy it to the clipboard, ready for pasting to...wherever you fancy. Plus, I've even created a .REG for you that adds the script to Explorer's context menu. Obviously, you'll need to edit the script path in the .REG.

To answer the inevitable question, no, there is no guarantee, warranty or support of any kind.

REGEDIT4

[HKEY_CLASSES_ROOT\Msi.Package\shell\GetProductCode]
@="Get Product &Code"

[HKEY_CLASSES_ROOT\Msi.Package\shell\GetProductCode\command]
@="cscript c:\\windows\\syswow64\\GetProductCode.VBS \"%1%\""

.VBS:

Option Explicit

Dim blnResult
Dim strMSI
Dim strCode

strMSI   = WScript.Arguments(0)
If Len(strMSI) = 0 Then
 WScript.Quit(False)
End If

blnResult  = GetMSIProductCode(strMSI, strCode)
If blnResult Then
 WScript.Echo strMSI & "'s ProductCode is " & strCode
 blnResult = CopyToClipboard(strCode)
End If

Function GetMSIProductCode(ByVal strMSIFile, ByRef strProductCode)
 Const intMSI_OpenDatabaseModeReadOnly  = 0  '// Opens a database read-only, no persistent changes
 Const intMSI_OpenDatabaseModeTransact  = 1  '// Opens a database read/write in transaction mode
 Const intMSI_OpenDatabaseModeDirect  = 2  '// Opens a database direct read/write without transaction
 Const intMSI_OpenDatabaseModeCreate  = 3  '// Creates a new database, transact mode read/write
 Const intMSI_OpenDatabaseModeCreateDirect = 4  '// Creates a new database, direct mode read/write
 Const intMSI_OpenDatabaseModeListScript  = 5  '// Opens a database to view advertise script files, such as the files generated by the CreateAdvertiseScript method
 Const intMSI_OpenDatabaseModePatchFile  = 32  '// Adds this flag to indicate a patch file

 GetMSIProductCode = False
 
 If strMSIFile = "" Then
  Exit Function
 End If
 
 Dim objInstaller
 Dim objDatabase
 Dim objView
 Dim objRecord

 On Error Resume Next
 Set objInstaller  = CreateObject("WindowsInstaller.Installer")
 If Not IsObject(objInstaller) Then
  Exit Function
 End If
 
 Set objDatabase  = objInstaller.OpenDatabase(strMSIFile, intMSI_OpenDatabaseModeReadOnly)
 If Err.Number Then
  Exit Function
 End If
 
 Set objView   = objDatabase.OpenView("Select `Value` From Property WHERE `Property` ='ProductCode'")
 objView.Execute

 Set objRecord   = objView.Fetch
 If Not objRecord Is Nothing Then
  strProductCode = objRecord.StringData(1)
  GetMSIProductCode = True
 End If

 Set objRecord   = Nothing
 Set objView   = Nothing
 Set objDatabase  = Nothing
 Set objInstaller  = Nothing
End Function

Function CopyToClipboard(ByVal strText)
 Dim objIE
 
 CopyToClipboard   = False
 
 Set objIE    = CreateObject("InternetExplorer.Application")
 If Not IsObject(objIE) Then
  Exit Function
 End If
 
 With objIE
  .Navigate("about:blank")
  .document.parentwindow.clipboardData.SetData "text", strText
  .Quit
 End With
 
 CopyToClipboard   = True
 
 Set objIE    = Nothing
End Function




Comments

  • I have just forwarded this link to some people. In SCCM if you deploy CMDs for example, you can set a detection method, if you choose MSI, it gives you a browse box, select your MSI it finds the GUID. But this might be useful to some people with diff tools. - Badger 8 years ago
  • This will be very handy, thanks for sharing and shorting that fidgety step. - rileyz 7 years ago
This post is locked
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ