Class: HawatelPS::Windows::WmiCli

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(namespace = nil) ⇒ WmiCli

Init WMI namespace

Parameters:

  • namespace (String) (defaults to: nil)

    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_namespaceWIN32OLE (private)

Connect to the WMI on the local server

Examples:

connect_to_namespace

Returns:

  • (WIN32OLE)

Raises:

  • (WIN32OLERuntimeError)

    if problem with connection to 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.

Examples:

results = @session.ExecQuery(wql_query)
wmi_ole_instances = create_wmi_ole_instances(results)

Parameters:

  • dataset (WMI32OLE)

    List of WMI objects

Returns:



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

Examples:

WmiCli.new.query('SELECT * FROM Win32_Process')

Parameters:

  • wql_query (String)

    WMI Query Language string

Returns:

Raises:



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