Class: Inspec::Resources::WMI
- Inherits:
-
Object
- Object
- Inspec::Resources::WMI
- Includes:
- ObjectTraverser
- Defined in:
- lib/inspec/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
-
#content ⇒ Object
Returns the value of attribute content.
Instance Method Summary collapse
-
#initialize(wmiclass = nil, opts = nil) ⇒ WMI
constructor
A new instance of WMI.
-
#method_missing(*keys) ⇒ Object
returns nil, if not existant or value.
- #params ⇒ Object
- #to_s ⇒ Object
- #value(key) ⇒ Object
Methods included from ObjectTraverser
Constructor Details
#initialize(wmiclass = nil, opts = nil) ⇒ WMI
Returns a new instance of WMI.
26 27 28 29 30 31 32 33 34 |
# File 'lib/inspec/resources/wmi.rb', line 26 def initialize(wmiclass = nil, opts = nil) @options = opts || {} if wmiclass.is_a?(Hash) @options.merge!(wmiclass) else Inspec.deprecate(:wmi_non_hash_usage, "Using `wmi('wmisclass')` is deprecated. Please use`wmi({class: 'wmisclass'})`") @options[:class] = wmiclass end 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/inspec/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 { |x| x.to_s.downcase } if keys.is_a?(Array) value(keys) end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
24 25 26 |
# File 'lib/inspec/resources/wmi.rb', line 24 def content @content end |
Instance Method Details
#params ⇒ Object
52 53 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 |
# File 'lib/inspec/resources/wmi.rb', line 52 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 = <<-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 |
#to_s ⇒ Object
90 91 92 |
# File 'lib/inspec/resources/wmi.rb', line 90 def to_s "WMI with #{@options}" end |
#value(key) ⇒ Object
48 49 50 |
# File 'lib/inspec/resources/wmi.rb', line 48 def value(key) extract_value(key, params) end |