簡體   English   中英

如何檢查是否安裝了特定的 MSI?

[英]How do i check if a particular MSI is installed?

我正在編寫一個 powershell 腳本,它將為我的 web 應用程序安裝一些依賴項。 在我的腳本中,我經常遇到檢查是否安裝了特定應用程序的問題。 似乎有一種獨特的方法可以檢查每個應用程序是否存在一個應用程序(即:通過檢查此文件夾的存在或 c: 上的此文件)。 有沒有辦法通過查詢已安裝的應用程序列表來檢查應用程序是否已安裝?

這是我有時使用的代碼(不是太頻繁,所以......)。 有關詳細信息,請參閱幫助注釋。

<#
.SYNOPSIS
    Gets uninstall records from the registry.

.DESCRIPTION
    This function returns information similar to the "Add or remove programs"
    Windows tool. The function normally works much faster and gets some more
    information.

    Another way to get installed products is: Get-WmiObject Win32_Product. But
    this command is usually slow and it returns only products installed by
    Windows Installer.

    x64 notes. 32 bit process: this function does not get installed 64 bit
    products. 64 bit process: this function gets both 32 and 64 bit products.
#>
function Get-Uninstall
{
    # paths: x86 and x64 registry keys are different
    if ([IntPtr]::Size -eq 4) {
        $path = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    }
    else {
        $path = @(
            'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
            'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
        )
    }

    # get all data
    Get-ItemProperty $path |
    # use only with name and unistall information
    .{process{ if ($_.DisplayName -and $_.UninstallString) { $_ } }} |
    # select more or less common subset of properties
    Select-Object DisplayName, Publisher, InstallDate, DisplayVersion, HelpLink, UninstallString |
    # and finally sort by name
    Sort-Object DisplayName
}

Get-Uninstall

要獲取已安裝應用程序的列表,請嘗試:

$r = Get-WmiObject Win32_Product | Where {$_.Name -match 'Microsoft Web Deploy' }
if ($r -ne $null) { ... }

有關詳細信息,請參閱Win32_Product上的文檔。

讓你的腳本掃描:

  • HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall
set-location HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
Get-ChildItem | foreach-object { $_.GetValue("DisplayName") }

您可以使用Test-Path通過查看注冊表中相應的卸載鍵來查看是否安裝了 MSI。

if (Test-Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{9BCA2118-F753-4A1E-BCF3-5A820729965C}') {
    Write-Output 'IIS URL Rewrite Module 2 is already installed.'
} else {
    Write-Output 'IIS URL Rewrite Module 2 is not yet installed.'
}

您必須插入與您的 MSI 相對應的 GUID。 您可以通過注冊表編輯器瀏覽卸載下的條目來找到 GUID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM