web-dev-qa-db-ja.com

プログラムがインストールされているかどうかを確認し、インストールされていない場合はどのようにインストールしますか?

整合性チェックのため、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以外のものとしてカウントされます。

7
Joshua Fletcher
$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
}
29
Jez