PowerShell:在不安装的情况下从 MSI 文件中获取 MSI 产品代码?

PowerShell: Get the MSI product code out of a MSI file without installing?

是否可以在不使用 PowerShell 安装 MSI 文件的情况下从 MSI 文件中检索 MSI 产品代码?我想将一个MSI文件的产品代码与安装在机器上的MSI代码进行比较,以确定该文件是否在过去安装过。

这里是读取商品代码的脚本,根据this article:

$path = "pathto.msi"

$comObjWI = New-Object -ComObject WindowsInstaller.Installer
$MSIDatabase = $comObjWI .GetType().InvokeMember("OpenDatabase","InvokeMethod",$Null,$comObjWI,@($Path,0))
$Query = "SELECT Value FROM Property WHERE Property = 'ProductCode'"
$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)

$Value 现在包含产品代码。

从 MSI 包中获取 ProductCode 的更简单方法:

Get-AppLockerFileInformation -Path "C:\PathTo\my.msi" | select -ExpandProperty Publisher | Select BinaryName