Created
April 11, 2011 04:43
-
-
Save jstangroome/913062 to your computer and use it in GitHub Desktop.
Read the ProductVersion from a Windows Installer MSI file via PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
No worries @DoublNAT