Search code examples
sqlwindows-installer

List Tables in an msi file?


How does one use SQL to list the tables in an MSI file?


Solution

  • MSI SDK: The _Tables table is a read-only system table that lists all the tables in the database. Query this table to find out if a table exists.

    Adapting the script WiExport.vbs from the Windows Installer Scripting Examples from the Windows Installer SDK you get something like the below.


    MSI SDK VBScripts: In order to find WiExport.vbs: With Visual Studio installed, look under: %ProgramFiles(x86)%\Windows Kits\10\bin\10.0.17763.0\x86 (adjust version numbers for your current installation) (or just search for it on github.com).


    Sample Screen Shot: Here is a sample run of the script below:

    enter image description here

    Sample: Procedure for script:

    • 1) save as ListMSITables.vbs on desktop (link to github.com)
    • 2) drag-and-drop an MSI file onto the VBScript
    • 3) a message box will show number of tables, and table names

    Note: Very large MSI files could make the message box overflow the screen. Just press any key to dismiss (I use ESC).

    On Error Resume Next
    Const msiOpenDatabaseModeReadOnly = 0
    
    Dim installer : Set installer = Nothing
    Set installer = Wscript.CreateObject("WindowsInstaller.Installer")
    Dim counter : counter = 0
    
    ' Verify incoming drag and drop arguments
    If WScript.Arguments.Count = 0 Then MsgBox "Drag and drop an MSI file onto the VBScript" End If
    filename = Wscript.Arguments(0)
    If (Right (LCase(filename),3) <> "msi") Then 
       WScript.Quit
    End If
    
    Dim database : Set database = installer.OpenDatabase(filename, msiOpenDatabaseModeReadOnly)
    Dim table, view, record
    
    Set view = database.OpenView("SELECT `Name` FROM _Tables")
    view.Execute
    
    Do
        Set record = view.Fetch
        If record Is Nothing Then Exit Do
        table = record.StringData(1)
        tables = tables + table + vbNewLine
        counter = counter + 1
    Loop
    
    MsgBox "Number of tables: " + CStr(counter) + vbNewLine + vbNewLine + tables
    
    Set view = Nothing
    

    Github.com: The above is obviously VBScript. Just pillage github.com for more of the same, in all kinds of languages.


    Links: