Class: Inspec::Resources::WMI

Inherits:
Object
  • Object
show all
Includes:
ObjectTraverser
Defined in:
lib/resources/wmi.rb

Overview

This resource simplifies the access to wmi on CLI you would use: WMIC /NAMESPACE:\rootrsopcomputer PATH RSOP_SecuritySettingNumeric WHERE “KeyName = ‘MinimumPasswordAge’ And precedence=1” GET Setting We use Get-WmiObject via Powershell to retrieve all values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ObjectTraverser

#extract_value

Constructor Details

#initialize(wmiclass, opts = {}) ⇒ WMI

Returns a new instance of WMI.



27
28
29
30
31
32
33
34
# File 'lib/resources/wmi.rb', line 27

def initialize(wmiclass, opts = {})
  # verify that this resource is only supported on Windows
  return skip_resource 'The `windows_feature` resource is not supported on your OS.' unless inspec.os.windows?

  @wmiclass = wmiclass
  @wminamespace = opts[:namespace]
  @wmifilter = opts[:filter]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*keys) ⇒ Object

returns nil, if not existant or value



37
38
39
40
41
42
43
44
45
46
# File 'lib/resources/wmi.rb', line 37

def method_missing(*keys)
  # catch behavior of rspec its implementation
  # @see https://github.com/rspec/rspec-its/blob/master/lib/rspec/its.rb#L110
  keys.shift if keys.is_a?(Array) && keys[0] == :[]

  # map all symbols to strings
  keys = keys.map(&:to_s) if keys.is_a?(Array)

  value(keys)
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



25
26
27
# File 'lib/resources/wmi.rb', line 25

def content
  @content
end

Instance Method Details

#infoObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/resources/wmi.rb', line 52

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

  # we should abort execution, if wmi class is not given or wmi resource is
  # executed on a non-windows system
  return @content if @wmiclass.nil?

  # optional params
  cmd_namespace = "-namespace #{@wminamespace}" unless @wminamespace.nil?
  cmd_filter = "-filter \"#{@wmifilter}\"" unless @wmifilter.nil?

  # run wmi command
  cmd = inspec.command("Get-WmiObject -class #{@wmiclass} #{cmd_namespace} #{cmd_filter} | ConvertTo-Json")
  @content = JSON.parse(cmd.stdout)
rescue JSON::ParserError => _e
  @content
end

#to_sObject



71
72
73
# File 'lib/resources/wmi.rb', line 71

def to_s
  "WMI #{@wmiclass} where #{@wmifilter}"
end

#value(key) ⇒ Object



48
49
50
# File 'lib/resources/wmi.rb', line 48

def value(key)
  extract_value(key, info)
end