class Inspec::Resources::RegistryKey

def registry_key(path)

def registry_key(path)
  return @registry_cache if defined?(@registry_cache)
  # load registry key and all properties
  script = <<-EOH
  Function InSpec-GetRegistryKey($path) {
    $reg = Get-Item ('Registry::' + $path)
    if ($reg -eq $null) {
      Write-Error "InSpec: Failed to find registry key"
      exit 1001
    }
    $properties = New-Object -Type PSObject
    $reg.Property | ForEach-Object {
        $key = $_
        $keytype = $key
        if ("(default)".Equals($key)) { $keytype = '' }
        $value = New-Object psobject -Property @{
          "value" =  $(Get-ItemProperty ('Registry::' + $path)).$key;
          "type"  = $reg.GetValueKind($keytype);
        }
        $properties | Add-Member NoteProperty $_ $value
    }
    $properties
  }
  $path = '#{path}'
  InSpec-GetRegistryKey($path) | ConvertTo-Json -Compress
  EOH
  cmd = inspec.powershell(script)
  # cannot rely on exit code for now, successful command returns exit code 1
  # return nil if cmd.exit_status != 0, try to parse json
  begin
    if cmd.exit_status == 1001 && cmd.stderr =~ /InSpec: Failed to find registry key/
      # TODO: provide the stderr output
      @registry_cache = nil
    else
      @registry_cache = JSON.parse(cmd.stdout)
      # convert keys to lower case
      @registry_cache = Hash[@registry_cache.map do |key, value|
        [key.downcase, value]
      end]
    end
  rescue JSON::ParserError => _e
    @registry_cache = nil
  end
  @registry_cache
end