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 existent or value.
- #params ⇒ Object
- #resource_id ⇒ 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.
29 30 31 32 33 34 35 36 37 |
# File 'lib/inspec/resources/wmi.rb', line 29 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 existent or value
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/inspec/resources/wmi.rb', line 40 def method_missing(*keys) # catch behavior of rspec its implementation # @see https://github.com/rspec/rspec-its/blob/v1.2.0/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.
27 28 29 |
# File 'lib/inspec/resources/wmi.rb', line 27 def content @content end |
Instance Method Details
#params ⇒ Object
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 = <<-EOH Function Aggregate { $propsHash = @{} ForEach ($wmiObj in $Input) { ForEach ($wmiProp in $wmiObj.properties) { If($propsHash.ContainsKey($wmiProp.name)) { $propsHash[$wmiProp.name].add($wmiProp.value) | Out-Null } Else { $propsHash[$wmiProp.name] = [System.Collections.ArrayList]@($wmiProp.value) } } } $propsHash } 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 |
#resource_id ⇒ Object
98 99 100 |
# File 'lib/inspec/resources/wmi.rb', line 98 def resource_id @options[:class] || "WMI" end |
#to_s ⇒ Object
102 103 104 |
# File 'lib/inspec/resources/wmi.rb', line 102 def to_s "WMI with #{@options}" end |
#value(key) ⇒ Object
51 52 53 |
# File 'lib/inspec/resources/wmi.rb', line 51 def value(key) extract_value(key, params) end |