Upravit

Sdílet prostřednictvím


Retrieving a WMI Class

The first type of object you can retrieve is a WMI class. When retrieving a WMI class, you actually retrieve a class definition, which is a listing of the properties, qualifiers, and methods that fully describe the class. However, a class definition is basically the class itself.

PowerShell uses a standard query to retrieve class definitions, using the meta_class class.

To retrieve a class definition in PowerShell

  • Use the Get-WmiObject with a query to meta_class, with the WHERE clause containing the name of the class you with to retrieve.

    Get-WmiObject -query "SELECT * FROM meta_class WHERE __class = 'Win32_LogicalDisk'"
    

    Get-WmiObject is the standard cmdlet PowerShell uses to retrieve class and instance information from WMI. The meta_class class defines the query as a schema query. Without the meta_class class, this query would return all instances of Win32_LogicalDisk. For more information about querying WMI, see SELECT Statement for Schema Queries.

The current process for retrieving a WMI definition in C# is to use CIMInstance class.

To retrieve a class definition in C# (Microsoft.Management.Infrastructure)

  1. Using the Microsoft.Management.Infrastructure namespace, create a CIMInstance class with the specified namespace and class name.

    The created class will contain all of the class information, but no instance data.

    using Microsoft.Management.Infrastructure;
    ...
    string Namespace = @"root\cimv2";
    string className = "Win32_LogicalDisk";
    
    CimInstance diskDrive = new CimInstance(className, Namespace);
    
  2. Alternately, as with PowerShell, you could also perform a query, using the meta_class tag as part of the query.

    using Microsoft.Management.Infrastructure;
    ...
    string Namespace = @"root\cimv2";
    string diskDriveQuery = "SELECT * FROM meta_class WHERE __class = 'Win32_LogicalDisk'";
    
    CimSession mySession = CimSession.Create("localhost");
    IEnumerable<CimInstance> queryInstance = mySession.QueryInstances(Namespace, "WQL", diskDriveQuery);
    

As with PowerShell, C# uses a meta_class query to retrieve class definitions. Alternately, you can create a ManagementClass object to access the class definition directly.

Note

System.Management was the original .NET namespace used to access WMI; however, the APIs in this namespace generally are slower and do not scale as well relative to their more modern Microsoft.Management.Infrastructure counterparts.

 

To retrieve a class definition in C# (System.Management)

  1. You can use the ManagementObjectSerarcher with a query to meta_class, with the WHERE clause containing the name of the class you with to retrieve.

    using System.Management;
    ...
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM meta_class WHERE __class = 'Win32_LogicalDisk'");
    ManagementObjectCollection myDiskCollection = searcher.Get();
    

    ManagementObjectSerarcher is the standard class .NET uses to retrieve class and instance information from WMI. ManagementObjectSerarcher.Get returns a ManagementObjectCollection that contains the schema definition class. The meta_class class defines the query as a schema query. Without the meta_class class, this query would return all instances of Win32_LogicalDisk. For more information about querying WMI, see SELECT Statement for Schema Queries.

  2. Alternately, create a new ManagementClass object, with the name as the path, to retrieve the class.

    using System.Management;
    ...
    ManagementClass objInst = new ManagementClass("Win32_LogicalDisk");
    

You can retrieve a class definition in VBScript in a similar way to retrieving a specific instance.

To retrieve a class definition in VBScript

  1. Call SWbemServices.Get but do not identify a specific instance in the object path for the class.

  2. The following code example retrieves the class definition for the class that describes logical drives on your computer.

    Set objinst = GetObject("WinMgmts:Win32_LogicalDisk")
    

    Windows Script Host (WSH) also supports the following.

    <OBJECT id="myLocator" progid="WbemScripting.SWbemLocator"></OBJECT>
    

    On Active Server Pages (ASP) use GetObject or CreateObject in the server-side script. For more information, see Creating Active Server Pages for WMI.

  3. A class or instance can also be specified, in which case the returned object is a WMI object, for example, an instance of Win32_LogicalDisk, rather than a services object. Note that you cannot use the VBScript GetObject functions to create an instance of the generic object SWbemObject.

  4. In HTML pages running in Microsoft Internet Explorer (IE), GetObject and CreateObject can fail because WMI scripting objects, like ActiveX controls, are not marked as safe for scripting. The one exception is the SWbemDateTime object. The only way that these calls can succeed is when you lower the IE security settings, which is not recommended.

When retrieving a class in C++, call the IWbemServices version of GetObject.

To retrieve a class definition in C++

  1. Call the IWbemServices::GetObject or IWbemServices::GetObjectAsync methods to retrieve the definition of a class.
  2. One class can have multiple class definitions, which happens typically when you have more than one class provider loaded into one namespace. When a class has multiple class definitions, WMI returns the first definition discovered and the WBEM_S_DUPLICATE_OBJECTS status code.

Because GetObject returns a class definition, it is commonly used as the first step in creating an instance. For more information on how to use GetObject, see Creating and Declaring an Instance Using C++.