class Inspec::Resources::WMI
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.
Attributes
content[RW]
Public Class Methods
new(wmiclass = nil, opts = nil)
click to toggle source
# 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
Public Instance Methods
method_missing(*keys)
click to toggle source
returns nil, if not existent or value
# 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
params()
click to toggle source
# 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
to_s()
click to toggle source
# File lib/inspec/resources/wmi.rb, line 98 def to_s "WMI with #{@options}" end
value(key)
click to toggle source
# File lib/inspec/resources/wmi.rb, line 51 def value(key) extract_value(key, params) end
Private Instance Methods
lowercase_keys(content)
click to toggle source
# File lib/inspec/resources/wmi.rb, line 104 def lowercase_keys(content) if content.is_a?(Hash) content.keys.each do |key| new_key = key.to_s.downcase content[new_key] = content.delete(key) lowercase_keys(content[new_key]) end elsif content.respond_to?(:each) content.each { |item| lowercase_keys(item) } end content end