mirror of
https://github.com/inspec/inspec
synced 2024-11-10 15:14:23 +00:00
add wmi resource
This commit is contained in:
parent
1d7c110917
commit
d045927d28
2 changed files with 52 additions and 0 deletions
|
@ -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
51
lib/resources/wmi.rb
Normal 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
|
Loading…
Reference in a new issue