Kae Travis

Query a Windows Installer (MSI) using Powershell

This is just a quick example of how we can query a Windows Installer using Powershell code:

$msiOpenDatabaseModeReadOnly = 0
$msiOpenDatabaseModeTransact = 1
$windowsInstaller = New-Object -ComObject windowsInstaller.Installer
$pathToMSI = "C:\Users\xxxx\Desktop\AlkaneExample.msi"
$database = $windowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $null, $windowsInstaller, @($pathToMSI, $msiOpenDatabaseModeReadOnly))
$query = "SELECT Property, Value FROM Property"
$propView = $database.GetType().InvokeMember("OpenView", "InvokeMethod", $null, $database, ($query))
$propView.GetType().InvokeMember("Execute", "InvokeMethod", $null, $propView, $null) | Out-Null
$propRecord = $propView.GetType().InvokeMember("Fetch", "InvokeMethod", $null, $propView, $null)
while  ($propRecord -ne $null)
{
$col1 = $propRecord.GetType().InvokeMember("StringData", "GetProperty", $null, $propRecord, 1)
$col2 = $propRecord.GetType().InvokeMember("StringData", "GetProperty", $null, $propRecord, 2)
write-host $col1 - $col2
#fetch the next record
$propRecord = $propView.GetType().InvokeMember("Fetch", "InvokeMethod", $null, $propView, $null)	
}
$propView.GetType().InvokeMember("Close", "InvokeMethod", $null, $propView, $null) | Out-Null          
$propView = $null 
$propRecord = $null
$database = $null

PLEASE NOTE

When writing Powershell functions to return single values (a string, for example) please ensure you pipe any calls to ‘Execute’ or ‘Close’ to ‘Out-Null’ (please see above). This will stop the function from returning a System.Object[] (since Powershell functions return the whole output from the function, and not just the value after the ‘return’ statement)!

Query a Windows Installer (MSI) using Powershell
Query a Windows Installer (MSI) using Powershell

Leave a Reply