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

编程入门 行业动态 更新时间:2024-10-28 12:20:31
本文介绍了如何使用 PowerShell 查找 MSI 产品版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们的最终交付有很多 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.

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 产品版本号?

本文发布于:2023-05-27 02:23:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/275494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   版本号   产品   MSI   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!