Skip to content

Commit 08a9275

Browse files
author
Michal Zobec
committed
Create Get-MSIFileInformation.ps1
1 parent 5450780 commit 08a9275

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<#
2+
Author: Nickolaj Andersen
3+
Source: https://www.scconfigmgr.com/2014/08/22/how-to-get-msi-file-information-with-powershell/
4+
5+
Example
6+
.\Get-MSIFileInformation.ps1 -Path "D:\Source$\Apps\7-zip\7z920-x64.msi" -Property ProductCode
7+
#>
8+
9+
10+
11+
param(
12+
[parameter(Mandatory=$true)]
13+
[ValidateNotNullOrEmpty()]
14+
[System.IO.FileInfo]$Path,
15+
16+
[parameter(Mandatory=$true)]
17+
[ValidateNotNullOrEmpty()]
18+
[ValidateSet("ProductCode", "ProductVersion", "ProductName", "Manufacturer", "ProductLanguage", "FullVersion")]
19+
[string]$Property
20+
)
21+
Process {
22+
try {
23+
# Read property from MSI database
24+
$WindowsInstaller = New-Object -ComObject WindowsInstaller.Installer
25+
$MSIDatabase = $WindowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $null, $WindowsInstaller, @($Path.FullName, 0))
26+
$Query = "SELECT Value FROM Property WHERE Property = '$($Property)'"
27+
$View = $MSIDatabase.GetType().InvokeMember("OpenView", "InvokeMethod", $null, $MSIDatabase, ($Query))
28+
$View.GetType().InvokeMember("Execute", "InvokeMethod", $null, $View, $null)
29+
$Record = $View.GetType().InvokeMember("Fetch", "InvokeMethod", $null, $View, $null)
30+
$Value = $Record.GetType().InvokeMember("StringData", "GetProperty", $null, $Record, 1)
31+
32+
# Commit database and close view
33+
$MSIDatabase.GetType().InvokeMember("Commit", "InvokeMethod", $null, $MSIDatabase, $null)
34+
$View.GetType().InvokeMember("Close", "InvokeMethod", $null, $View, $null)
35+
$MSIDatabase = $null
36+
$View = $null
37+
38+
# Return the value
39+
return $Value
40+
}
41+
catch {
42+
Write-Warning -Message $_.Exception.Message ; break
43+
}
44+
}
45+
End {
46+
# Run garbage collection and release ComObject
47+
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WindowsInstaller) | Out-Null
48+
[System.GC]::Collect()
49+
}

0 commit comments

Comments
 (0)