Method: Inspec::Resources::WMI#params

Defined in:
lib/inspec/resources/wmi.rb

#paramsObject



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

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| i{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 = "  Function Aggregate {\n    $propsHash = @{}\n    ForEach ($wmiObj in $Input) {\n      ForEach ($wmiProp in $wmiObj.properties) {\n        If($propsHash.ContainsKey($wmiProp.name)) {\n          $propsHash[$wmiProp.name].add($wmiProp.value) | Out-Null\n        } Else {\n          $propsHash[$wmiProp.name] = [System.Collections.ArrayList]@($wmiProp.value)\n        }\n      }\n    }\n    $propsHash\n  }\n  Get-WmiObject \#{params} | Aggregate | ConvertTo-Json\n  EOH\n\n  # run wmi command\n  cmd = inspec.powershell(script)\n  @content = JSON.parse(cmd.stdout)\n\n  # make all keys case-insensitive\n  @content = lowercase_keys(@content)\nrescue JSON::ParserError => _e\n  @content\nend\n"