Class: HawatelPS::Windows::WmiCli::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/hawatel_ps/windows/wmi/wmi_instance.rb

Overview

Instance of Windows Management Instrumentation client

Container of single WIN32OLE object wih parameters of this object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wmi_ole_object) ⇒ Instance

Create instance of WmiClient class with WIN32OLE object

Parameters:

  • wmi_ole_object (WIN32OLE)

    Single WMI32OLE object



18
19
20
21
# File 'lib/hawatel_ps/windows/wmi/wmi_instance.rb', line 18

def initialize(wmi_ole_object)
  @wmi_ole_object = wmi_ole_object
  @properties = extract_props_to_hash(wmi_ole_object)
end

Instance Attribute Details

#propertiesHash (readonly)

Returns Properties from WIN32OLE object.

Returns:

  • (Hash)

    Properties from WIN32OLE object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hawatel_ps/windows/wmi/wmi_instance.rb', line 13

class Instance
  attr_reader :wmi_ole_object, :properties

  # Create instance of WmiClient class with WIN32OLE object
  # @param wmi_ole_object [WIN32OLE] Single WMI32OLE object
  def initialize(wmi_ole_object)
    @wmi_ole_object = wmi_ole_object
    @properties = extract_props_to_hash(wmi_ole_object)
  end

  # Execute WIN32OLE method
  # @see https://msdn.microsoft.com/en-us/library/windows/desktop/aa393774
  # @param name [String] WMI method name assigned to WIN32OLE object
  # @example Object if from Win32_Process class and it has 'GetOwner' method
  #   wmi_object.execMethod('GetOwner')
  # @return [WIN32OLE]
  def execMethod(name)
    result = @wmi_ole_object.execMethod_(name) if @wmi_ole_object.ole_respond_to?('execMethod_')
  rescue WIN32OLERuntimeError => ex
    raise WmiCliException, :exception => ex, :message => "Cannot invoke execMethod_('#{name}')"
  end

  private

  # Get property from WIN32OLE object
  # @param wmi_object [WIN32OLE] WMI object
  # @example
  #   extract_props_to_hash(wmi_object)
  # @return [Hash]
  def extract_props_to_hash(wmi_obj)
    properties = {}
    #binding.pry
    if wmi_obj.ole_respond_to?('properties_')
      wmi_obj.properties_.each do |property|
        properties[property.name.downcase.to_sym] = property.value
      end
      properties.freeze
    end
  end

end

#wmi_ole_objectWIN32OLE (readonly)

Returns WMI Object.

Returns:

  • (WIN32OLE)

    WMI Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hawatel_ps/windows/wmi/wmi_instance.rb', line 13

class Instance
  attr_reader :wmi_ole_object, :properties

  # Create instance of WmiClient class with WIN32OLE object
  # @param wmi_ole_object [WIN32OLE] Single WMI32OLE object
  def initialize(wmi_ole_object)
    @wmi_ole_object = wmi_ole_object
    @properties = extract_props_to_hash(wmi_ole_object)
  end

  # Execute WIN32OLE method
  # @see https://msdn.microsoft.com/en-us/library/windows/desktop/aa393774
  # @param name [String] WMI method name assigned to WIN32OLE object
  # @example Object if from Win32_Process class and it has 'GetOwner' method
  #   wmi_object.execMethod('GetOwner')
  # @return [WIN32OLE]
  def execMethod(name)
    result = @wmi_ole_object.execMethod_(name) if @wmi_ole_object.ole_respond_to?('execMethod_')
  rescue WIN32OLERuntimeError => ex
    raise WmiCliException, :exception => ex, :message => "Cannot invoke execMethod_('#{name}')"
  end

  private

  # Get property from WIN32OLE object
  # @param wmi_object [WIN32OLE] WMI object
  # @example
  #   extract_props_to_hash(wmi_object)
  # @return [Hash]
  def extract_props_to_hash(wmi_obj)
    properties = {}
    #binding.pry
    if wmi_obj.ole_respond_to?('properties_')
      wmi_obj.properties_.each do |property|
        properties[property.name.downcase.to_sym] = property.value
      end
      properties.freeze
    end
  end

end

Instance Method Details

#execMethod(name) ⇒ WIN32OLE

Execute WIN32OLE method

Examples:

Object if from Win32_Process class and it has ‘GetOwner’ method

wmi_object.execMethod('GetOwner')

Parameters:

  • name (String)

    WMI method name assigned to WIN32OLE object

Returns:

  • (WIN32OLE)

See Also:



29
30
31
32
33
# File 'lib/hawatel_ps/windows/wmi/wmi_instance.rb', line 29

def execMethod(name)
  result = @wmi_ole_object.execMethod_(name) if @wmi_ole_object.ole_respond_to?('execMethod_')
rescue WIN32OLERuntimeError => ex
  raise WmiCliException, :exception => ex, :message => "Cannot invoke execMethod_('#{name}')"
end

#extract_props_to_hash(wmi_obj) ⇒ Hash (private)

Get property from WIN32OLE object

Examples:

extract_props_to_hash(wmi_object)

Parameters:

  • wmi_object (WIN32OLE)

    WMI object

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
51
# File 'lib/hawatel_ps/windows/wmi/wmi_instance.rb', line 42

def extract_props_to_hash(wmi_obj)
  properties = {}
  #binding.pry
  if wmi_obj.ole_respond_to?('properties_')
    wmi_obj.properties_.each do |property|
      properties[property.name.downcase.to_sym] = property.value
    end
    properties.freeze
  end
end