From 1cf40fe00e65ac2e6911bce64154e5111687b90d Mon Sep 17 00:00:00 2001 From: Adam Edwards Date: Thu, 15 May 2014 14:14:01 -0700 Subject: Delay connection to first query, not constructor --- lib/wmi-lite/wmi.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/wmi-lite/wmi.rb b/lib/wmi-lite/wmi.rb index 1acc9bd..cc6492d 100644 --- a/lib/wmi-lite/wmi.rb +++ b/lib/wmi-lite/wmi.rb @@ -22,7 +22,8 @@ require 'wmi-lite/wmi_instance' module WmiLite class Wmi def initialize(namespace = nil) - @connection = new_connection(namespace.nil? ? 'root/cimv2' : namespace) + @namespace = namespace + @connection = nil end def query(wql_query) @@ -54,6 +55,7 @@ module WmiLite private def start_query(wql_query) + connect_to_namespace result = @connection.ExecQuery(wql_query) raise_if_failed(result) result @@ -66,9 +68,12 @@ module WmiLite result.count end - def new_connection(namespace) - locator = WIN32OLE.new("WbemScripting.SWbemLocator") - locator.ConnectServer('.', namespace) + def connect_to_namespace + if @connection.nil? + namespace = @namespace.nil? ? 'root/cimv2' : @namespace + locator = WIN32OLE.new("WbemScripting.SWbemLocator") + @connection = locator.ConnectServer('.', namespace) + end end def wmi_result_to_snapshot(wmi_object) -- cgit v1.2.1