DSCResources/ROACommon/Src/Get-InstalledProductEntry.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function Get-InstalledProductEntry {
<#
    .NOTES
        https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/dev/DSCResources/MSFT_xPackageResource/MSFT_xPackageResource.psm1
#>

    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String] $Name,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String] $IdentifyingNumber,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String] $InstalledCheckRegHive = 'LocalMachine',

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String] $InstalledCheckRegKey,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String] $InstalledCheckRegValueName,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String] $InstalledCheckRegValueData
    )
    process {

        $uninstallKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';
        $uninstallKeyWow64 = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall';

        if ($IdentifyingNumber) {

            $keyLocation = '{0}\{1}' -f $uninstallKey, $identifyingNumber;
            $item = Get-Item $keyLocation -ErrorAction SilentlyContinue;
            if (-not $item) {

                $keyLocation = '{0}\{1}' -f $uninstallKeyWow64, $identifyingNumber;
                $item = Get-Item $keyLocation -ErrorAction SilentlyContinue;
            }
            return $item;
        }

        foreach ($item in (Get-ChildItem -ErrorAction Ignore $uninstallKey, $uninstallKeyWow64)) {

            $installedProduct = Get-LocalizableRegistryKeyValue -RegistryKey $item -ValueName 'DisplayName';
            if ($installedProduct) {

                $installedProduct = $installedProduct.Trim();
            }
            if ($Name -eq $installedProduct) {

                return $item;
            }
        }

        if ($InstalledCheckRegKey -and $InstalledCheckRegValueName -and $InstalledCheckRegValueData) {

            $installValue = $null;
            $getRegistryValueIgnoreErrorParams = @{
                RegistryHive = $InstalledCheckRegHive;
                Key = $InstalledCheckRegKey;
                Value = $InstalledCheckRegValueName;
            }

            #if 64bit OS, check 64bit registry view first
            if ([System.Environment]::Is64BitOperatingSystem) {

                $installValue = Get-RegistryValueIgnoreError @getRegistryValueIgnoreErrorParams -RegistryView [Microsoft.Win32.RegistryView]::Registry64;
            }

            if ($null -eq $installValue) {

                $installValue = Get-RegistryValueIgnoreError @getRegistryValueIgnoreErrorParams -RegistryView [Microsoft.Win32.RegistryView]::Registry32;
            }

            if ($installValue) {

                if ($InstalledCheckRegValueData -and $installValue -eq $InstalledCheckRegValueData) {
                    
                    return @{ Installed = $true; }
                }
            }
        }

        return $null;

    } #end process
} #end function Get-InstalledProductEntry