WMI: Windows Management Instrumentation Browser |
|
products |
|
WMI Browser Download source code for this article WMI Scripting Library:The WMI scripting library provides a consistent set of controls (in the form of automation objects) that allow you to access and manage WMI managed resources. It doesn't matter whether you're managing computers, event logs, the operating system, processes, services, or pick your favorite resource; the objects in the WMI scripting library always work the same. The consistency provided by the automation objects in the WMI scripting library is best conveyed by the finite set of tasks you can perform using the WMI scripting library. You can create seven basic script types using the WMI scripting library.
Once you understand a template well enough to manage one type of WMI managed resource, you can easily adapt the same template to manage hundreds of other WMI managed resources.
WMI Scripting Library Object ModelThe WMI scripting library is your control panel to the entire WMI infrastructure. The WMI scripting library is implemented in a single automation component named wbemdisp.dll that physically resides in the %SystemRoot%\system32\wbem directory. Actually, the WMI scripting library consists of twenty-four automation objects (nineteen in Windows 2000 and earlier). You, however, do not have to learn all the gory details of all those automation objects. In addition to the twenty-four automation objects in the Microsoft Windows XP and Windows Server 2003 version of wbemdisp.dll, the scripting library also contains thirteen enumerations. In many ways, you can compare the automation objects in the WMI scripting library to the core interfaces provided by ADSI. The ADSI core interfaces—IADs and IADsContainer, for example—provide a consistent approach to scripting objects in the Active Directory, irrespective of an object's class and attributes. Similarly, the automation objects in the WMI scripting library provide a consistent and uniform scripting model for WMI managed resources. I found a sample essay, possibly better Essay Writers, geography papers. The WMI scripting library provides the general purpose set of automation objects scripts used to authenticate and connect to WMI, and subsequently access instances of WMI managed resources. Once you obtain an instance of a WMI managed resource using the WMI scripting library, you can access the methods and properties defined by the managed resource's class definition—as if the methods and properties were part of the scripting library itself. "more..." Utilizing the WMI Scripting Library Object ModelThe WMI scripting library object model provides a great deal of insight into the mechanics of how WMI scripting works. Calling the SWbemLocator ConnectServer method returns a SWbemServices object. Calling the SWbemServices ExecNotificationQuery method returns a SWbemEventSource object. Also, calling the SWbemServices ExecQuery or InstancesOf method returns a SWbemObjectSet collection. And calling the SWbemServices Get method returns a SWbemObject. In our Demo Application we have instantiated a SWbemServices object called oService, as below: Set oService = GetObject("winmgmts:\\.\") You can replace the "." with the computer name for which you want to browse the WMI Objects. Now you can query oService object to get the collection of Instances and Sub-Classes, as below:
NB: The flag (1 + &H10) is
InstancesOf and SubclassesOf always returns a SWbemObjectSet collection. Each managed resource instance in a SWbemObjectSet collection is represented by a SWbemObject. SWbemServices is the object that represents an authenticated connection to a WMI namespace on a local or remote computer. Additionally, SWBemServices plays an important role in every WMI script. For example, you use the SWbemServices InstancesOf method to retrieve all instances of a managed resource. Similarly, you use the SWbemServices ExecQuery method combined with a WQL (WMI Query Language) query to retrieve all or a subset of instances of a managed resource. And you use the SWbemServices ExecNotificationQuery method to subscribe to events that represent changes in the managed environment. A SWbemObjectSet is a collection of zero or more SWbemObject objects. Zero, because it's possible for a computer to have zero instances of, say, a tape drive (modeled by Win32_TapeDrive). Each SWbemObject in a SWbemObjectSet can represent one of two things:
SWbemObject is the multiple-identity object that takes on the identity of the resource you're managing. For example, if you retrieve instances of the Win32_Process managed resource, SWbemObject takes on an identity modeled after the Win32_Process class definition. Whereas, if you retrieve instances of the Win32_Service managed resource, SWbemObject takes on an identity modeled after the Win32_Service class. Our Demo Application then iterates through these collection to fabricate an ADO Recordset, as below:
Dim rs As Object
Dim oStr As String
Dim I As Long
Set rs = CreateObject("ADODB.Recordset")
With rs
.Fields.Append "ID", 200, 2000
.Fields.Append "NameIs", 200, 2000
End With
rs.Open
Set NavRS2 = rs
Set oSet = oService.InstancesOf(sStr, 1 + &H10)
For Each iObj In oSet
oStr = Mid(iObj.Path_, InStrRev(iObj.Path_, ":") + 1)
With rs
.AddNew
I = I + 1
.Fields(0) = "" & iObj.Path_
.Fields(1) = "" & oStr
.Update
End With
If (I > RecordLimit) Then Exit For
Next iObj
rs.MoveFirst
Note that we have capped the retrieval of records by a settable
parameter "RecordLimit". This would prevent huge resultsets
eating up the computer's resources. You may set to a reasonable value of
200 or so.
Important: Of the twenty-four automation objects in the WMI scripting library, the three most important—that is, the three you should learn first—are SWbemServices, SWbemObjectSet, and SWbemObject. Why? Because SWbemServices, SWbemObjectSet, and SWbemObject are an essential part of darn near every WMI script. In fact, if we revisit our ADSI analogy, SWbemServices, SWbemObjectSet, and SWbemObject are to WMI scripting what IADs and IADsContainer are to ADSI scripting. Displaying the Properties of a Managed ResourceThe code fabricates an ADO recordset with 4 fields and populates them with the SWbemObject Properties. On Error Resume Next
Dim rs As Object
Dim oStr As String
Dim iProp As Object
Dim iQual As Object
Dim I As Long
Set rs = CreateObject("ADODB.Recordset")
With rs
.Fields.Append "Name", 200, 2000
.Fields.Append "Origin", 200, 2000
.Fields.Append "Value", 200, 2000
.Fields.Append "Qualifiers", 200, 2000
End With
rs.Open
Set WebRS1 = rs
rs.Sort "Name"
Set oObj = oService.Get(sStr)
For Each iProp In oObj.Properties_
With rs
.AddNew
I = I + 1
.Fields(0) = iProp.Name
.Fields(1) = iProp.Origin
.Fields(2) = iProp.Value & " "
oStr = ""
For Each iQual In iProp.Qualifiers_
oStr = oStr & iQual.Name & "=" & iQual.Value & vbCrLf
Next iQual
.Fields(3) = oStr
.Update
End With
If (I > RecordLimit) Then Exit For
Next iProp
rs.MoveFirst
You can even List the names of Properties, Methods, Qualifiers and Derivation and fabricate an ADO recodset out of it (shown below) On Error Resume Next
Dim rs As Object
Dim oStr As String
Dim iDer()
Dim iMethod As Object
Dim iProp As Object
Dim iQual As Object
Dim I As Long
'Fabricate Recordset
Set rs = CreateObject("ADODB.Recordset")
With rs
.Fields.Append "Properties", 200, 2000
.Fields.Append "Methods", 200, 2000
.Fields.Append "Qualifiers", 200, 2000
.Fields.Append "Derivation", 200, 2000
End With
rs.Open
Set WebRS2 = rs
Set oObj = oService.Get(sStr)
rs.AddNew
'Property List
oStr = ""
For Each iProp In oObj.Properties_
oStr = oStr & iProp.Name & vbCrLf
Next iProp
rs.Fields(0) = oStr
'Method List
oStr = ""
For Each iMethod In oObj.Methods_
oStr = oStr & iMethod.Name & vbCrLf
Next iMethod
rs.Fields(1) = oStr
'Qualifier List
oStr = ""
For Each iQual In oObj.Qualifiers_
oStr = oStr & iQual.Name & vbCrLf
Next iQual
rs.Fields(2) = oStr
'Derivation
iDer = oObj.Derivation_
oStr = ""
For I = LBound(iDer) To UBound(iDer)
oStr = oStr & iDer(I) & vbCrLf
Next I
rs.Fields(3) = oStr
rs.Update
rs.MoveFirst
ProvideRecordset Event Private Sub Low calories1_ProvideRecordset(ByVal sSQL As String, sRS As Object)
TreeView1.MousePointer = 13
Set sRS = oData.GetData(sSQL)
TreeView1.MousePointer = 0
End Sub
The oData object is the instantiation of a class clsWMI which has been coded
to take-care-of of all the WMI related codes as explained above. It fabricates
the ADO recordsets for a given SQL (?) command. Please see
the schema in table XNav and XWeb of the wmi.mdb database to
understand the information flow. The class can be extended to include more features and greater reporting.
|