首页 文章

使用PowerShell使用升级代码查找产品版本

提问于
浏览
0

我必须使用他们的upgrade codes更新一些产品(MSI),我有所有这些产品的列表' upgrade codes. Now to push updates I need to compare every product' s版本 .

如何在这种情况下找到产品版本?

喜欢:

gwmi win32_product | Where-Object {$_.Name -like "name"}

但是这个使用了名称,我想只使用升级代码找到版本 .

1 回答

  • 0

    在PowerShell中完成所需内容的最简单方法是使用以下WMI查询来获取属于UpgradeCode系列的软件包 .

    $UpgradeCode = '{AA783A14-A7A3-3D33-95F0-9A351D530011}'
    $ProductGUIDs = @(Get-WmiObject -Class Win32_Property | Where-Object {$_.Property -eq 'UpgradeCode' -and $_.value -eq $UpgradeCode}).ProductCode
    
    Get-WmiObject -Class Win32_Product | Where-Object {$ProductGUIDs -Contains $_.IdentifyingNumber}
    

    它的唯一缺点是Win32_Property和Win32_Product类都很慢,如果时间不是一个很大的因素,你可以使用它 . 如果您需要更快的性能,您可以从注册表中获得类似的信息

    function Decode-GUID {
        param( [string]$GUID )
        $GUIDSections = @( 8, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2 ) 
        $Position = 0
        $result = @()
    
        ForEach($GUIDSection In $GUIDSections)
        { $arr = $GUID.SubString($Position, $GUIDSection) -split ""; 
        [array]::Reverse($arr);
        $result = $result +($arr -join '').replace(' ',''); 
        $Position += $GUIDSection } 
        return "{$(($result -join '').Insert(8,'-').Insert(13, '-').Insert(18, '-').Insert(23, '-'))}"
    }
    
    function Encode-GUID {
        param( [string]$GUID )
        $GUID = $GUID -creplace '[^0-F]'
        $GUIDSections = @( 8, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2 ) 
    
        $Position = 0
        $result = ""
    
        ForEach($GUIDSection In $GUIDSections)
        { $arr = $GUID.substring($Position, $GUIDSection) -split ""; 
        [array]::Reverse($arr);
        $result = $result + ($arr -join '').replace(' ',''); 
        $Position += $GUIDSection } 
        return $result
    }
    
    #Enter the UpgradeCode here
    $UpgradeCode = Encode-GUID "{AA783A14-A7A3-3D33-95F0-9A351D530011}"
    
    $ProductGUIDs = (Get-Item HKLM:"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes\$UpgradeCode", HKLM:"SOFTWARE\Classes\Installer\UpgradeCodes\$UpgradeCode").Property |
        Select-Object -Unique |
        ForEach-Object {Decode-GUID $_}
    
    Get-ChildItem HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
        Where-Object {$ProductGUIDs -contains $_.PSChildName} |
        Get-ItemProperty |
        Select-Object -Property @{Name='PackageCode';Expression={$_.PSChildName}}, DisplayName, Publisher, DisplayVersion, InstallDate
    

    在注册表中,"Installer"部分中使用的GUID被编码以匹配C本身使用它们的方式 . 上例中的解码和编码功能基于Roger Zander blog post中使用的技术 . 请原谅一些代码的混乱,如果您需要解释的任何部分,请告诉我 . 希望这对你有所帮助 .

相关问题