如何使用 PowerShell 查找 MSI 产品版本号? [英] How do I find the MSI product version number using PowerShell?
问题描述
我们的最终交付有很多 MSI 文件.
Our end deliverable has lot of MSI files.
我会确保他们的产品名称和产品版本是否正确.
I would ensure whether they has correct product name and product version.
我正在使用 Orca 和手动完成.
I am using Orca and doing it manually.
如何使用 PowerShell 执行此操作?
How to do it using PowerShell?
推荐答案
这应该是一个简单的答案...从 Windows Installer 开始有一个 COM 对象 你可以使用:
This should have been an easy answer... To start with Windows Installer has a COM object you can use:
ProgID:WindowsInstaller.Installer
但是,当您使用 PowerShell 创建对象时,您不会获得任何属性或方法:
However when you create an object out of with PowerShell you don't get any of the properties or methods:
$object = New-Object -Com WindowsInstaller.Installer
$object | gm
...没什么:-(
显然这是 PowerShell 及其类型适应系统的问题.请参阅此博文以解决问题.
Apparently this is a problem with PowerShell and its type adapting system. See this blog post for a work around.
http://www.snowland.se/2010/02/21/read-msi-information-with-powershell/
如果您使用 VBScript,就不应该有这个问题.
If you use VBScript you shouldn't have this problem.
这里有一些 VBScript 可以得到我找到的版本:
Here's some VBScript that will get the version I found:
Const msiOpenDatabaseModeReadOnly = 0
Dim msi, db, view
Set msi = CreateObject("WindowsInstaller.Installer")
Set db = msi.OpenDataBase("C:\Users\andy\Desktop\Module.msi", msiOpenDatabaseModeReadOnly)
Set view = db.OpenView("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductVersion'")
Call view.Execute()
GetVersion = view.Fetch().StringData(1)
Wscript.Echo GetVersion
您可以从 PowerShell 调用它:
You can call this from PowerShell:
$version = & cscript.exe /nologo GetVersion.vbs
更新!这种类型的适应问题让我很沮丧,我对 VBS 解决方案不满意.经过一番研究,我找到了一种在 PowerShell 中正确执行此操作的方法.我改编了他的博客条目一>.享受!
Update! This type adaption problem was frustrating me and I wasn't happy with the VBS solution. After a bit of research I found a way to do this in PowerShell proper. I adapted code from his blog entry. Enjoy!
function Get-MsiDatabaseVersion {
param (
[string] $fn
)
try {
$FullPath = (Resolve-Path $fn).Path
$windowsInstaller = New-Object -com WindowsInstaller.Installer
$database = $windowsInstaller.GetType().InvokeMember(
"OpenDatabase", "InvokeMethod", $Null,
$windowsInstaller, @($FullPath, 0)
)
$q = "SELECT Value FROM Property WHERE Property = 'ProductVersion'"
$View = $database.GetType().InvokeMember(
"OpenView", "InvokeMethod", $Null, $database, ($q)
)
$View.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View, $Null)
$record = $View.GetType().InvokeMember(
"Fetch", "InvokeMethod", $Null, $View, $Null
)
$productVersion = $record.GetType().InvokeMember(
"StringData", "GetProperty", $Null, $record, 1
)
$View.GetType().InvokeMember("Close", "InvokeMethod", $Null, $View, $Null)
return $productVersion
} catch {
throw "Failed to get MSI file version the error was: {0}." -f $_
}
}
Get-MsiDatabaseVersion "Installer.msi"
这篇关于如何使用 PowerShell 查找 MSI 产品版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!