Class: WMI::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-wmi/base.rb

Class Method Summary collapse

Class Method Details

.all(options = {}) ⇒ Object

an alias for find(:all)



107
108
109
# File 'lib/ruby-wmi/base.rb', line 107

def all(options={})
  find(:all, options)
end

.find(arg = :all, options = {}) ⇒ Object

WMI::Win32ComputerSystem.find(:all)

  returns an array of Win32ComputerSystem objects

WMI::Win32ComputerSystem.find(:first)
  returns the first Win32ComputerSystem object

WMI::Win32ComputerSystem.find(:last)
  returns the last Win32ComputerSystem object

options:

  :conditions

   Conditions can either be specified as a string, array, or hash representing the WHERE-part of an SQL statement.
   The array form is to be used when the condition input is tainted and requires sanitization. The string form can
   be used for statements that don't involve tainted data. The hash form works much like the array form, except
   only equality and range is possible. Examples:

     Win32ComputerSystem.find(:all, :conditions => {:drivetype => 3} )  # Hash
     Win32ComputerSystem.find(:all, :conditions => [:drivetype, 3] )   # Array
     Win32ComputerSystem.find(:all, :conditions => 'drivetype = 3' )   # String

  :host       - computername, defaults to localhost
  :class      - swebm class , defaults to 'root\\cimv2'
  :privileges - see WMI::Privilege for a list of privileges
  :user       - username (domain\\username)
  :password   - password


87
88
89
90
91
92
93
94
# File 'lib/ruby-wmi/base.rb', line 87

def find(arg=:all, options={})
  set_connection options
  case arg
    when :all; find_all(options)
    when :first; find_first(options)
    when :last; find_last(options)
  end
end

.find_by_wql(query) ⇒ Object

#find_by_wql currently only works when called through #find it may stay like that too. I haven’t decided.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby-wmi/base.rb', line 46

def find_by_wql(query)
  d = connection.ExecQuery(query)
  begin
    d.count # needed to check for errors.  Weird, but it works.
  rescue => error
    case error.to_s
    when /Invalid class/i ; raise InvalidClass
    when /Invalid query/i ; raise InvalidQuery

    end
  end
  d.to_a
end

.first(options = {}) ⇒ Object

an alias for find(:first)



102
103
104
# File 'lib/ruby-wmi/base.rb', line 102

def first(options={})
  find(:first, options)
end

.last(options = {}) ⇒ Object

an alias for find(:last)



97
98
99
# File 'lib/ruby-wmi/base.rb', line 97

def last(options={})
  find(:last, options)
end

.set_connection(options = {}) ⇒ Object



111
112
113
114
115
116
# File 'lib/ruby-wmi/base.rb', line 111

def set_connection(options={})
  @host = options[:host]
  @klass = options[:class] || 'root\\cimv2'
  @user,@password = options[:user], options[:password]
  @privileges = options[:privileges]
end

.set_wmi_class_name(name) ⇒ Object



118
119
120
# File 'lib/ruby-wmi/base.rb', line 118

def set_wmi_class_name(name)
  @subclass_name = name
end

.subclass_nameObject



122
123
124
# File 'lib/ruby-wmi/base.rb', line 122

def subclass_name
  @subclass_name ||= self.name.split('::').last
end