As mentioned in the previous post in this series, there are two distinct mechanisms that can be used to establish a connection to WMI on a local/remote computer:
1) SWbemLocator object and
2) SWbemServices Object
Both the mechanisms are same except 2 differences. SwbemLocator object has to be used to connect to WMI if:
a) username and password is required to connect to remote computer (if local machine account has less privileges than that of remote machine)
b) WMI script is required to run from within the webpage
The sample statements illustrating both the connection mechanisms:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strComputer = "PC-Vijay"
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", "domain\vijay", "password")
WMI Connection Mechanism
As seen in both of the above illustrations, both return SWbemServices object after connecting to cimv2 WMI namespace on strComputer. After obtaining the reference to an SWbemServices object, any of the several methods can be called to connect to Managed Object in order to query it. It can return either of the SWbemObjectSet, SWbemObject, or SWbemEventSource object where each object represents the different entities listed below.
i) SWbemObject: Managed Object
ii) SWbemObjectset: Collection of Managed Objects
iii) SWbemEventSource: Collection of Events from event query
In the next post, we will examine sample script line by line in order to understand its components.