Obtaining MSI Product Version with Powershell 1.0

Want to know what version of a product is in an MSI? You need the ProductVersion property.

It’s a bit tricky to get at from Powershell 1.0, because there’s no runtime type info for the Installer class.

The following isn’t pretty, but it does the job.

#
# Fetches ProductVersion from MSI file
#
param($msiPath)
if($msiPath -eq $null)
{
"Expects full path to MSI file"
return;
}

# Create Installer instance
$installer = New-Object -comObject WindowsInstaller.Installer

# Call Installer.OpenDatabase(name, openMode)
$database = $installer.GetType().InvokeMember("OpenDatabase", [System.Reflection.BindingFlags]::InvokeMethod, $null, $installer, ($msiPath, 0))

# Call Database.OpenView(sql)
$view = $database.GetType().InvokeMember("OpenView", [System.Reflection.BindingFlags]::InvokeMethod, $null, $database, "SELECT `Value` FROM `Property` WHERE `Property` = 'ProductVersion'")

# Call View.Execute()
$view.GetType().InvokeMember("Execute", [System.Reflection.BindingFlags]::InvokeMethod, $null, $view, $null)

# Call View.Fetch()
$record = $view.GetType().InvokeMember("Fetch", [System.Reflection.BindingFlags]::InvokeMethod, $null, $view, $null)

# Get Record.StringData(field)
$productVersion = $record.GetType().InvokeMember("StringData", [System.Reflection.BindingFlags]::GetProperty, $null, $record, 1)

Write-Host $productVersion

Leave a Reply

Your email address will not be published. Required fields are marked *