Last active
April 15, 2020 05:36
-
-
Save stuarthallows/27e4267c4331e4d2564262b2040b77f3 to your computer and use it in GitHub Desktop.
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
# | |
# Script to extract a ProductId from an MSI file. | |
# | |
# Author: Nickolaj AndersenAugust | |
# https://www.scconfigmgr.com/2014/08/22/how-to-get-msi-file-information-with-powershell/ | |
# | |
param( | |
[parameter(Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[System.IO.FileInfo]$Path, | |
[parameter(Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[ValidateSet("ProductCode", "ProductVersion", "ProductName", "Manufacturer", "ProductLanguage", "FullVersion")] | |
[string]$Property | |
) | |
Process { | |
try { | |
# Read property from MSI database | |
$WindowsInstaller = New-Object -ComObject WindowsInstaller.Installer | |
$MSIDatabase = $WindowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $null, $WindowsInstaller, @($Path.FullName, 0)) | |
$Query = "SELECT Value FROM Property WHERE Property = '$($Property)'" | |
$View = $MSIDatabase.GetType().InvokeMember("OpenView", "InvokeMethod", $null, $MSIDatabase, ($Query)) | |
$View.GetType().InvokeMember("Execute", "InvokeMethod", $null, $View, $null) | |
$Record = $View.GetType().InvokeMember("Fetch", "InvokeMethod", $null, $View, $null) | |
$Value = $Record.GetType().InvokeMember("StringData", "GetProperty", $null, $Record, 1) | |
# Commit database and close view | |
$MSIDatabase.GetType().InvokeMember("Commit", "InvokeMethod", $null, $MSIDatabase, $null) | |
$View.GetType().InvokeMember("Close", "InvokeMethod", $null, $View, $null) | |
$MSIDatabase = $null | |
$View = $null | |
# Return the value | |
return $Value | |
} | |
catch { | |
Write-Warning -Message $_.Exception.Message ; break | |
} | |
} | |
End { | |
# Run garbage collection and release ComObject | |
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WindowsInstaller) | Out-Null | |
[System.GC]::Collect() | |
} | |
# | |
# Save the script as Get-MSIFileInformation.ps1 to C:\Scripts. | |
# | |
# 1. Open a PowerShell console and browse to C:\Scripts. | |
# 2. Run the following command: | |
# | |
# .\Get-MSIFileInformation.ps1 -Path "D:\Source$\Apps\7-zip\7z920-x64.msi" -Property ProductCode | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment