Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract ProductCode from msi package?

How to extract ProductCode from msi package? I want to use it later to uninstall msi via msiexec as described here

like image 330
ks1322 Avatar asked Feb 15 '11 14:02

ks1322


2 Answers

I can think of dozens of ways to do it. What programming languages do you currently use and/or comfortable with?

Take a look at

Execute SQL Statements

You could use WiRunSQL.vbs ( provided in the Platform SDK ) to run the command:

cscript /nologo WiRunSQL.vbs FOO.msi "SELECT Value FROM Property WHERE Property = 'ProductCode'"
like image 54
Christopher Painter Avatar answered Dec 26 '22 11:12

Christopher Painter


I wrote a Powershell function that I use when generating MSI-based Chocolatey packages at work, to detect if our internal package is installing a program that was already installed via other means:

function Get-MsiProductCode {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [ValidateScript({$_ | Test-Path -PathType Leaf})]
        [string]$Path
    )

    function Get-Property ( $Object, $PropertyName, [object[]]$ArgumentList ) {
        return $Object.GetType().InvokeMember($PropertyName, 'Public, Instance, GetProperty', $null, $Object, $ArgumentList)
    }

    function Invoke-Method ( $Object, $MethodName, $ArgumentList ) {
        return $Object.GetType().InvokeMember( $MethodName, 'Public, Instance, InvokeMethod', $null, $Object, $ArgumentList )
    }

    $ErrorActionPreference = 'Stop'
    Set-StrictMode -Version Latest

    #http://msdn.microsoft.com/en-us/library/aa369432(v=vs.85).aspx
    $msiOpenDatabaseModeReadOnly = 0
    $Installer = New-Object -ComObject WindowsInstaller.Installer

    $Database = Invoke-Method $Installer OpenDatabase $Path, $msiOpenDatabaseModeReadOnly

    $View = Invoke-Method $Database OpenView "SELECT Value FROM Property WHERE Property='ProductCode'"

    [void]( Invoke-Method $View Execute )

    $Record = Invoke-Method $View Fetch
    if ( $Record ) {
        Get-Property $Record StringData 1
    }

    [void]( Invoke-Method $View Close @() )
    Remove-Variable -Name Record, View, Database, Installer
}
like image 36
Bender the Greatest Avatar answered Dec 26 '22 12:12

Bender the Greatest