Class: HawatelPS::Windows::WmiCli
- Inherits:
-
Object
- Object
- HawatelPS::Windows::WmiCli
- Defined in:
- lib/hawatel_ps/windows/wmi/wmi_cli.rb,
lib/hawatel_ps/windows/wmi/wmi_instance.rb
Overview
Windows Management Instrumentation Client
This is wrapper for WMI with limited functionalities (only call queries)
Defined Under Namespace
Classes: Instance
Instance Method Summary collapse
-
#connect_to_namespace ⇒ WIN32OLE
private
Connect to the WMI on the local server.
-
#create_wmi_ole_instances(dataset) ⇒ Array<WmiCli::Instance>
private
Create the WMI32OLE instance from a data set.
-
#initialize(namespace = nil) ⇒ WmiCli
constructor
Init WMI namespace.
-
#query(wql_query) ⇒ WmiCli::Instance
Call WMI query.
Constructor Details
#initialize(namespace = nil) ⇒ WmiCli
Init WMI namespace
14 15 16 17 |
# File 'lib/hawatel_ps/windows/wmi/wmi_cli.rb', line 14 def initialize(namespace = nil) @namespace = namespace.nil? ? 'root/cimv2' : namespace @session = nil end |
Instance Method Details
#connect_to_namespace ⇒ WIN32OLE (private)
Connect to the WMI on the local server
41 42 43 44 45 46 47 48 |
# File 'lib/hawatel_ps/windows/wmi/wmi_cli.rb', line 41 def connect_to_namespace if @session.nil? locator = WIN32OLE.new("WbemScripting.SWbemLocator") @session = locator.ConnectServer('.', @namespace) end rescue WIN32OLERuntimeError => ex raise WmiCliException, :exception => ex, :message => "Cannot connect to namespace '#{@namespace}'." end |
#create_wmi_ole_instances(dataset) ⇒ Array<WmiCli::Instance> (private)
Create the WMI32OLE instance from a data set.
56 57 58 59 60 61 62 |
# File 'lib/hawatel_ps/windows/wmi/wmi_cli.rb', line 56 def create_wmi_ole_instances(dataset) instances = [] dataset.each do |wmi_object| instances.push(Instance.new(wmi_object)) end instances end |
#query(wql_query) ⇒ WmiCli::Instance
Call WMI query
25 26 27 28 29 30 31 32 |
# File 'lib/hawatel_ps/windows/wmi/wmi_cli.rb', line 25 def query(wql_query) connect_to_namespace results = @session.ExecQuery(wql_query) wmi_ole_instances = create_wmi_ole_instances(results) rescue WIN32OLERuntimeError => ex raise WmiCliException, :exception => ex, :message => "Wrong result from WQL Query = '#{wql_query}'." end |