Skip to content

Instantly share code, notes, and snippets.

@NicklausBrain
Forked from jstangroome/Msi.psm1
Created April 6, 2018 13:43
Show Gist options
  • Save NicklausBrain/be15e0798b07e96c9bff3f0c54c5abbc to your computer and use it in GitHub Desktop.
Save NicklausBrain/be15e0798b07e96c9bff3f0c54c5abbc to your computer and use it in GitHub Desktop.
Read the ProductVersion from a Windows Installer MSI file via PowerShell
function Get-MsiProductVersion {
[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='ProductVersion'")
Invoke-Method $View Execute
$Record = Invoke-Method $View Fetch
if ($Record) {
Write-Output (Get-Property $Record StringData 1)
}
Invoke-Method $View Close @()
Remove-Variable -Name Record, View, Database, Installer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment