PowerShell比较.exe版本和.msi版本



早上好,我正在编写一个脚本,将安装的。exe版本与另一个位置的。msi版本进行比较。如果版本不匹配,它将安装较新的。msi。我必须编写代码来获取版本号,但它一直说它们不匹配;他们会,但也不会。

.EXE Version is 9.1.3.3108
.MSI Version is 9.1.3
下面是我的代码
Function Get-MsiDBVersion {
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 $_
}
}
$RACToolsVersion = Get-MsiDBVersion "C:SourceBuildSourceLaptop2-AdminCenterRACToolsRACTools_x64.msi"
if((get-item "C:Program FilesDellSysMgtrac5racadm.exe").VersionInfo.FileVersion -match $RACToolsVersion){
>>     write-host "yes match"
>> }else{
>>     write-host "no match"}

当我运行这个时,我总是得到no match

的结果

我以前遇到过这个问题-见这里:https://www.alkanesolutions.co.uk/2016/12/13/query-windows-installer-msi-using-powershell/

,特别是底部的注释!

"当编写Powershell函数返回单个值(例如字符串)时,请确保将任何对' Execute '或' Close '的调用都管道到' Out-Null '(请参见上文)。这将阻止该函数返回System。对象[](因为Powershell函数返回函数的整个输出,而不仅仅是' return '语句后面的值)!">

因此,在您的情况下,您的函数返回前导空格,可以像这样修复(注意out-null):

Function Get-MsiDBVersion {
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) | Out-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) | Out-Null

return $productVersion
} catch {
throw "Failed to get MSI file version the error was: {0}." -f $_
}
}

相关内容

  • 没有找到相关文章

最新更新