2011-01-20 115 views
6

我正在寫一個powershell腳本,將爲我的webapp安裝一些依賴關係。在我的腳本中,我遇到了一個反覆檢查特定應用程序是否安裝的問題。似乎有一種獨特的方式來檢查每個應用程序是否存在應用程序(即:通過檢查此文件夾或此文件的現有存在:)。是否沒有辦法通過查詢已安裝的應用程序列表來檢查應用程序是否已安裝?我如何檢查是否安裝了特定的MSI?

回答

10

這裏是我有時使用的代碼(不要過於頻繁,所以...)。詳細信息請參閱幫助註釋。

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

讓你的腳本掃描:

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

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

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

參見Win32_Product的文檔獲取更多信息。

+0

請注意,如果在安裝程序數據庫中發現錯誤,Get-WmiObject Win32_Product可能會修改目標系統。這很簡單,並且是一個非常有用的功能。但是,如果您不僅僅是使用它來檢測已安裝的應用程序,還可以做好更多的工作。 – 2014-09-03 13:38:49