Question

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

Was it helpful?

Solution

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'"

OTHER TIPS

You could achieve a similar effect by doing the following in PowerShell based on the installed programs:

Get-WmiObject -Class Win32_Product -Filter "Vendor LIKE 'The Company%' AND Name LIKE '%The Product%'" | %{ 
    Write-Host "Uninstalling $($_.IdentifyingNumber)"
    $_.Uninstall() 
}

(Obv the tighter the query, the quicker it will run - the LIKE above is very expensive)

Or you could apply the general technique in here on your stack.

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top