2011-01-20 34 views
6

Estoy escribiendo un script de powershell que instalará algunas dependencias para mi aplicación web. En mi script, me estoy encontrando con un problema recurrente al verificar si una aplicación en particular está instalada. parece que hay una forma única de verificar si existe una aplicación para cada aplicación (es decir, al verificar la existencia de esta carpeta o este archivo en c :). ¿No hay una forma de que pueda verificar si una aplicación se instala consultando una lista de aplicaciones instaladas?¿Cómo verifico si un MSI particular está instalado?

Respuesta

10

Este es el código que utilizo a veces (no muy a menudo, así que ...). Ver los comentarios de ayuda para más detalles.

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

Haga que su exploración de la escritura:

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

Para obtener una lista de las aplicaciones instaladas Proveedores:

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

Consulte la documentación sobre Win32_Product para obtener más información.

+0

Tenga en cuenta que Get-WmiObject Win32_Product puede modificar el sistema de destino si encuentra errores en su base de datos del instalador. Es simple y una función realmente útil. Pero si está utilizando esto para algo más que simplemente detectar aplicaciones instaladas, prepárese para hacer más trabajo. –

Cuestiones relacionadas