如何使用PowerShell查找MSI产品版本号?

Sam*_*abu 11 powershell powershell-2.0

我们的最终交付物有很多MSI文件.

我会确保他们是否有正确的产品名称和产品版本.

我正在使用Orca并手动完成.

如何使用PowerShell做到这一点?

And*_*ndi 25

这应该是一个简单的答案...从Windows Installer开始有一个COM对象,您可以使用:

ProgID:WindowsInstaller.Installer

但是,当您使用PowerShell创建对象时,您不会获得任何属性或方法:

$object = New-Object -Com WindowsInstaller.Installer
$object | gm
Run Code Online (Sandbox Code Playgroud)

...没有 :-(

显然这是PowerShell及其类型适配系统的问题.有关解决方法,请参阅此博客文章.

http://www.snowland.se/2010/02/21/read-msi-information-with-powershell/

如果您使用VBScript,则不应该出现此问题.

编辑:

这是一些VBScript,它将获得我找到的版本:

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
Run Code Online (Sandbox Code Playgroud)

您可以从PowerShell中调用它:

$version = & cscript.exe /nologo GetVersion.vbs
Run Code Online (Sandbox Code Playgroud)

更新!这种类型的适应问题令我感到沮丧,我对VBS解决方案不满意.经过一些研究后,我发现了一种在PowerShell中正确执行此操作的方法.我从他的博客条目中修改了代码.请享用!

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"
Run Code Online (Sandbox Code Playgroud)