DSCResources/ROACommon/Src/Get-RegistryValueIgnoreError.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
function Get-RegistryValueIgnoreError {
<#
    .NOTES
        https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/dev/DSCResources/MSFT_xPackageResource/MSFT_xPackageResource.psm1
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [Microsoft.Win32.RegistryHive] $RegistryHive,

        [Parameter(Mandatory)]
        [System.String] $Key,

        [Parameter(Mandatory)]
        [System.String] $Value,

        [Parameter(Mandatory)]
        [Microsoft.Win32.RegistryView] $RegistryView
    )
    process {

        try {

            $baseKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey($RegistryHive, $RegistryView);
            $subKey =  $baseKey.OpenSubKey($Key);
            if ($null -ne $subKey) {

                return $subKey.GetValue($Value);
            }
        }
        catch {

            $exceptionText = ($_ | Out-String).Trim();
            Write-Verbose "Exception occured in Get-RegistryValueIgnoreError: $exceptionText";
        }
        
        return $null;

    } #end process
} #end function Get-RegistryValueIgnoreError