Read MSI information with PowerShell
I tried to search for some way to read MSI-information with PowerShell, that wasn’t to easy to find.
Anyway, here is a function that helps you read the “Property” view from one MSI-file, the result is stored in a hash table.
# Load some TypeData $SavedEA = $Global:ErrorActionPreference $Global:ErrorActionPreference = "SilentlyContinue" Update-TypeData -AppendPath ((Split-Path -Parent $MyInvocation.MyCommand.Path) + "\comObject.types.ps1xml") $Global:ErrorActionPreference = $SavedEA function global:get-msiproperties { PARAM ( [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="MSI Database Filename",ValueFromPipeline=$true)] [Alias("Filename","Path","Database","Msi")] $msiDbName ) # A quick check to see if the file exist if(!(Test-Path $msiDbName)){ throw "Could not find " + $msiDbName } # Create an empty hashtable to store properties in $msiProps = @{} # Creating WI object and load MSI database $wiObject = New-Object -com WindowsInstaller.Installer $wiDatabase = $wiObject.InvokeMethod("OpenDatabase", (Resolve-Path $msiDbName).Path, 0) # Open the Property-view $view = $wiDatabase.InvokeMethod("OpenView", "SELECT * FROM Property") $view.InvokeMethod("Execute") # Loop thru the table $r = $view.InvokeMethod("Fetch") while($r -ne $null) { # Add property and value to hash table $msiProps[$r.InvokeParamProperty("StringData",1)] = $r.InvokeParamProperty("StringData",2) # Fetch the next row $r = $view.InvokeMethod("Fetch") } $view.InvokeMethod("Close") # Return the hash table return $msiProps }
You need to expand your type configuration with this file, saved as comObject.types.ps1xml in the same directory as the script above.
System.__ComObject GetProperty SetProperty InvokeParamProperty InvokeMethod
2010-03-08 09:55
Now that’s what I call helpful.
Thanks a lot Rikard!
2010-05-03 15:13
Thank you very much for this code sample.
2010-06-21 14:48
Missing at the end, before returning
2010-06-21 19:07
Thanks Ferdi, the script is updated.