powershell - 如何检查程序是否已安装,如果未安装,如何安装?

标签 powershell if-statement installation 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
        }

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

最佳答案

$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
}

关于powershell - 如何检查程序是否已安装,如果未安装,如何安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712686/

相关文章:

python - 安装 Python telnetlib 模块

powershell - 将参数传递到 Powershell

c# - 如何设置 PowerShell Cmdlet 的默认输出格式?

python - 如何强制 pip install --editable 创建轮缓存而不是egg?

ios - Swift:如何比较 "if"子句中的可选值

c - 删除 "if"语句的大括号是否有任何异常(exception)?

c# - 编辑安装程序类中的自定义配置部分

r - 如何在R中使用powershell删除csv文件中的行?

powershell - `PowerShellVersion` 和 `PowerShellHostVersion` 之间的区别

java - 我应该在哪里初始化变量才能在 IF block 中工作?