如何检查是否安装了特定的MSI?

时间:2011-01-20 21:51:39

标签: powershell

我正在编写一个PowerShell脚本,它将为我的webapp安装一些依赖项。在我的脚本中,我遇到了一个反复检查是否安装了特定应用程序的问题。似乎有一种独特的方法来检查每个应用程序是否存在应用程序(例如:通过检查此文件夹的现有文件或c :)上的此文件。有没有办法通过查询已安装的应用程序列表来检查是否安装了应用程序?

4 个答案:

答案 0 :(得分:13)

要获取已安装应用程序的列表,请尝试:

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

有关详细信息,请参阅Win32_Product上的文档。

答案 1 :(得分:11)

以下是我有时使用的代码(不常见,所以......)。有关详细信息,请参阅帮助说明。

<#
.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

答案 2 :(得分:4)

让您的脚本扫描:

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

答案 3 :(得分:0)

您可以使用 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。