add wmi resource

This commit is contained in:
Christoph Hartmann 2016-03-01 21:01:52 +01:00
parent 1d7c110917
commit d045927d28
2 changed files with 52 additions and 0 deletions

View file

@ -99,6 +99,7 @@ require 'resources/user'
require 'resources/vbscript'
require 'resources/windows_feature'
require 'resources/xinetd'
require 'resources/wmi'
require 'resources/yum'
# file formats, depend on json implementation

51
lib/resources/wmi.rb Normal file
View file

@ -0,0 +1,51 @@
# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter
class WMI < Inspec.resource(1)
name 'wmi'
desc 'request wmi information'
example "
describe wmi('RSOP_SecuritySettingNumeric', {
namespace: 'root\\rsop\\computer',
filter: 'KeyName = 'MinimumPasswordAge' And precedence=1'
}) do
its('Setting') { should eq true }
end
"
# This would be the same as:
# WMIC /NAMESPACE:\\root\rsop\computer PATH RSOP_SecuritySettingNumeric WHERE "KeyName = 'MinimumPasswordAge' And precedence=1" GET Setting
attr_accessor :content
def initialize(wmiclass, opts)
@wmiclass = wmiclass
@wminamespace = opts[:namespace]
@wmifilter = opts[:filter]
# 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?
end
# returns nil, if not existant or value
def method_missing(property)
info[property.to_s]
end
def to_s
"WMI #{@wmiclass} where #{@wmifilter}"
end
def info
return @content if defined?(@content)
@content = {}
cmd = inspec.command("Get-WmiObject -namespace #{@wminamespace} -class #{@wmiclass} -filter \"#{@wmifilter}\" | ConvertTo-Json")
@content = JSON.parse(cmd.stdout)
# sometimes we get multiple values
if @content.is_a?(Array)
# select the first entry
@content = @content.first
end
rescue JSON::ParserError => e
@content
end
end