Class: Inspec::Resources::WMI
- Inherits:
-
Object
- Object
- Inspec::Resources::WMI
- 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
-
#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.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/resources/wmi.rb', line 28 def initialize(wmiclass = nil, opts = nil) # 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? @options = opts || {} # if wmiclass is not a hash, we have to handle deprecation behavior if wmiclass.is_a?(Hash) @options.merge!(wmiclass) else warn '[DEPRECATION] `wmi(\'wmiclass\')` is deprecated. Please use `wmi({class: \'wmiclass\'})` instead.' @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
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/resources/wmi.rb', line 43 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.
26 27 28 |
# File 'lib/resources/wmi.rb', line 26 def content @content end |
Instance Method Details
#params ⇒ Object
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 |
# File 'lib/resources/wmi.rb', line 58 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}\"" } # 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
95 96 97 |
# File 'lib/resources/wmi.rb', line 95 def to_s "WMI with #{@options}" end |
#value(key) ⇒ Object
54 55 56 |
# File 'lib/resources/wmi.rb', line 54 def value(key) extract_value(key, params) end |