Private/Get-RegistryValue.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
Function Get-RegistryValue {
    param (
        [parameter(Mandatory)][ValidateNotNullOrEmpty()][string] $KeyName,
        [parameter()][string] $ComputerName,
        [parameter()][string] $LogFile = '' ,
        [parameter()][string] $KeyValue,
        [parameter()][ValidateSet('LocalMachine','ClassesRoot','CurrentConfig','Users')][string] $AccessType = 'LocalMachine'
    )
    Write-Log -Message "(Get-RegistryValue) $($ComputerName), $($AccessType), $($keyname), $($keyvalue)" -LogFile $logfile
    try {
        $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($AccessType, $ComputerName)
        $RegKey= $Reg.OpenSubKey($keyname)
        if ($null -ne $RegKey) {
            try { $return = $RegKey.GetValue($keyvalue) }
            catch { $return = $null }
        }
        else { $return = $null }
        Write-Log -Message "Value returned $return" -LogFile $logfile
    }
    catch {
        $return = "ERROR: Unknown"
        $Error.Clear()
    }
    , $return
}