Method: Inspec::Resources::WMI#params

Defined in:
lib/resources/wmi.rb

#paramsObject



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
# File 'lib/resources/wmi.rb', line 54

def params
  return @content if defined?(@content)
  @content = {}

  # abort if no options are available
  return @content unless defined?(@options)

  # filter for supported options
  args = @options.select { |key, _value| [:class, :namespace, :query, :filter].include?(key) }

  # convert to Get-WmiObject arguments
  params = ''
  args.each { |key, value| params += " -#{key} \"#{value.gsub('"', '`"')}\"" }

  # run wmi command and filter empty wmi
  script = <<-EOH
  Filter Aggregate
  {
      $arr = @{}
      $_.properties | % {
          $arr.Add($_.name, $_.value)
      }
      $arr
  }
  Get-WmiObject #{params} | Aggregate | ConvertTo-Json
  EOH

  # run wmi command
  cmd = inspec.powershell(script)
  @content = JSON.parse(cmd.stdout)

  # make all keys case-insensitive
  @content = lowercase_keys(@content)
rescue JSON::ParserError => _e
  @content
end