How to check if a program is installed and install it if it is not?

后端 未结 1 1204
孤城傲影
孤城傲影 2021-01-02 17:24

I would rather not use WMI due the integrity check.

This is what I have that does not work:

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


        
相关标签:
1条回答
  • 2021-01-02 17:32
    $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
    }
    
    0 讨论(0)
提交回复
热议问题