如何检查程序是否已安装并安装(如果不是)?

Jos*_*her 6 powershell installation if-statement powershell-4.0

由于完整性检查,我宁愿不使用WMI.

这是我有的不起作用:

$tempdir = Get-Location
$tempdir = $tempdir.tostring()

$reg32 = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$reg64 = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"

if((Get-ItemProperty $reg32 | Select-Object DisplayName | Where-Object { $_.DisplayName -Like '*Microsoft Interop Forms*' } -eq $null) -Or (Get-ItemProperty $reg64 | Select-Object DisplayName | Where-Object { $_.DisplayName -Like '*Microsoft Interop Forms*' } -eq $null))
        {
        (Start-Process -FilePath $tempdir"\microsoft.interopformsredist.msi" -ArgumentList "-qb" -Wait -Passthru).ExitCode
        }
Run Code Online (Sandbox Code Playgroud)

它总是返回false.如果我将它切换到-ne $null它总是返回true,所以我知道它正在检测$null输出,即使我相信(但可能是错误的),Get-ItemProperty返回的结果应该算作除了之外的其他东西$null.

Jez*_*Jez 27

$tempdir = Get-Location
$tempdir = $tempdir.tostring()
$appToMatch = '*Microsoft Interop Forms*'
$msiFile = $tempdir+"\microsoft.interopformsredist.msi"
$msiArgs = "-qb"

function Get-InstalledApps
{
    if ([IntPtr]::Size -eq 4) {
        $regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    }
    else {
        $regpath = @(
            'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
            'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
        )
    }
    Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString |Sort DisplayName
}

$result = Get-InstalledApps | where {$_.DisplayName -like $appToMatch}

If ($result -eq $null) {
    (Start-Process -FilePath $msiFile -ArgumentList $msiArgs -Wait -Passthru).ExitCode
}
Run Code Online (Sandbox Code Playgroud)

  • 别客气.那么请你把它标记为答案吗?(这是向下箭头下方的勾号). (4认同)
  • @Bajan这是一种确定操作系统架构的方法.[IntPtr]属性的值在32位进程中为4,在64位进程中为8. (4认同)
  • @ChrisVermeijlen; $ msiArgs只是一个变量来存储你想要传递给MSI的参数,在上面的例子中我们只是传递"-qb".您可以存储任何其他MSI参数,例如"/ norestart ALLUSERS = 2"等." - wait -Passthru"是Start-Process cmdlet的参数:https://docs.microsoft.com/en-us/powershell/模块/ microsoft.powershell.management /启动过程?视图=的powershell-5.1 (3认同)