Muokkaa

Jaa


Retrieving a WMI Instance

Retrieving an instance is one of the most common retrieval procedures you are likely to perform in WMI. You can retrieve an existing instance or create a new unnamed instance. The WMI path to the existing instance is a required parameter. For more information, see Describing the Location of a WMI Object.

Note

When providing an instance, a provider may be unable to provide a value for certain properties. Unless otherwise stated in the property description, you cannot infer any meaning from an empty value. This is not to be confused with a string which has a NULL value. In this case, the value is populated. It is empty but has a value: NULL.

 

Retrieve a local copy of the instance with a call to the PowerShell Get-WmiObject cmdlet.

To retrieve an instance of a WMI class using PowerShell

  • You can retrieve specific instances using the -class and -filter parameters.

    Get-WmiObject -query "SELECT * FROM Win32_logicalDisk WHERE DeviceID = 'C:'"
    

You can retrieve a WMI instance using C# by creating a search object using CimInstance, and then filling it with the relevant key values, and then searching for that object with a CimSession.GetInstance call.

To retrieve an instance of a WMI class using C# (Microsoft.Management.Infrastructure)

  1. Using the Microsoft.Management.Infrastructure namespace, create a new CimInstance object with the relevant class name and namespace.

    using Microsoft.Management.Infrastructure;
    ...
    string Namespace = @"root\cimv2";
    string className = "Win32_LogicalDisk";
    
    CimInstance myDrive = new CimInstance(className, Namespace);
    
  2. Create a CimProperty that contains the name and value of the key property of the instance you wish to search for, and add that property to your class object.

    myDrive.CimInstanceProperties.Add(CimProperty.Create("DeviceID", "C:", CimFlags.Key));
    
  3. Retrieve the object from WMI with a CimSession.GetInstance call.

    CimSession mySession = CimSession.Create("localhost");
    CimInstance searchInstance = mySession.GetInstance(Namespace, myDrive);
    

You can retrieve either a specific WMI class instance, or a collection of WMI class instances, using classes in the System.Management namespace.

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 an instance of a WMI class using C# (System.Management)

  1. Retrieve a local copy of a specific instance by creating a new ManagementObject, with the name and specific instance value passed in though the ManagementPath parameter. You can then retrieve the instance data by explicitly calling ManagementObject.Get.

    using System.Management;
    ...
    ManagementObject objInst = new ManagementObject("Win32_LogicalDisk.DeviceID='C:'");
    objInst.Get();
    
  2. Alternately, you can retrieve all instances of a WMI class by searching for them with a ManagementObjectSearcher, and then enumerating through the returned ManagementObjectCollection.

    using System.Management;
    ...
    ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
    ManagementObjectCollection colDisks = mgmtObjSearcher.Get();
    
    foreach (ManagementObject objDisk in colDisks)
    {
       Console.WriteLine("Device ID : {0}", objDisk["DeviceID"]);
    }
    
    Console.ReadLine();
    

    You can implicitly call the Get method by accessing the instance. For more information, see Retrieving Part of a WMI Instance.

Retrieve a local copy of the instance with a call to the VBScript GetObject method.

To retrieve an instance of a WMI class using VBScript

  • Call GetObject with the object path of the instance as shown in the following example.

    Set objinst = GetObject("WinMgmts:Win32_LogicalDisk='C:'")
    

    Retrieving a specific instance requires giving a name as part of the object path.

In C++, call IWbemServices::GetObject.

To retrieve an instance of a WMI class using C++