Редактиране

Споделяне чрез


SELECT Statement for Schema Queries

Schema data queries use the SELECT statement with a syntax similar to that for data queries. The difference is the use of a special class called "meta_class", which identifies the query as a schema query.

The following example requests all class definitions that are within the current namespace.

SELECT * FROM meta_class

Schema queries only support "*". To narrow the scope of the definitions returned, a provider can add a WHERE clause that specifies a particular class.

The following example shows how to add a WHERE clause to specify a particular class.

SELECT * FROM meta_class WHERE __this ISA "Win32_LogicalDisk"

The special property called __this identifies the target class for a schema query. Note that the ISA operator must be used with the __this property to request definitions for the subclasses of the target class. The preceding query returns the definition for the Win32_LogicalDisk class and definitions for all of its subclasses.

The following example shows how to request a class definition for a single class by using the __Class system property.

SELECT * FROM meta_class WHERE __Class = "Win32_LogicalDisk"

This query is equivalent to calling the IWbemServices::GetObject or the IWbemServices::GetObjectAsync method with the object path parameter set to "Win32_LogicalDisk".

The following VBScript code sample retrieves all child classes of a top level WMI class. The __Dynasty WMI system property holds the name of the top-level class from which a class is derived, which you can use to retrieve all classes in a namespace derived from a top level class, including that class.

' Retrieve immediate child classes for Cim_DataFile

Set objWmi = GetObject ("winmgmts:root\cimv2")

Set colClasses = objWmi.ExecQuery _ 
    ("Select * From meta_class " _
    & "Where __Dynasty = 'Win32_CurrentTime'")

For Each objClass In colClasses 
    WScript.Echo objClass.SystemProperties_("__Class")
Next

The following VBScript retrieves an immediate child classes for a WMI class.

' Retrieve immediate child classes for Cim_DataFile

Set objWmi = GetObject ("winmgmts:root\cimv2")

Set colClasses = objWmi.ExecQuery _ 
    ("Select * From meta_class " _
    & "Where __Superclass = 'Cim_DataFile'")

For Each objClass In colClasses 
    WScript.Echo objClass.SystemProperties_("__Class")
Next

The following VBScript retrieves top level classes. For all the top level classes in a WMI namespace, the __Superclass system property is Null. Therefore, it is possible to retrieve the top level classes by searching for a Null superclass.

 Retrieve top level classes in root\cimv2

Set objWmi = GetObject ("winmgmts:root\cimv2")

Set colClasses = objWmi.ExecQuery _
    ("Select * From meta_class Where __Superclass Is Null")

For Each objClass In colClasses
    WScript.Echo objClass.SystemProperties_("__Class")