Updating an Entire Instance

The most common means of updating a WMI class instance is to update the entire instance at once. By updating an entire instance, WMI does not have to parse the instance into individual properties and send them to your application. Instead, WMI can simply send you the entire instance. When you finish, WMI can then copy your entire changed instance over the original instance.

The following procedure describes how to modify or update an instance using PowerShell.

To modify or update an instance using PowerShell

  1. Retrieve a local copy of the object with a call to Get-WmiObject.

    $mySettings = get-WMIObject Win32_WmiSetting
    
  2. If necessary, view the properties of the object with a call to the Properties collection.

    Although not required, you may wish to know the value of the property before you change it.

    $mySettings.Properties
    
  3. Make any changes to the local object properties.

    Doing so changes only the local copy. To save your changes to WMI, you must place the entire copy back into the WMI repository.

    $mySettings.LoggingLevel = 1
    
  4. Place the object back into the WMI repository using a call to the Put method.

    $mySettings.Put()
    

The following procedure describes how to modify or update an instance using C#.

To modify or update an instance using C# (Microsoft.Management.Infrastructure)

  1. Retrieve a local copy of the object with a call to CimSession.GetInstance, as described in Retrieving a WMI Instance.

    using Microsoft.Management.Infrastructure;
    ...
    string Namespace = @"root\cimv2";
    string className = "win32_logicalDisk";
    
    CimInstance diskDrive = new CimInstance(className, Namespace);
    diskDrive.CimInstanceProperties.Add(CimProperty.Create("DeviceID", "C:", CimFlags.Key));
    
    CimSession session = CimSession.Create("localhost");
    CimInstance myDisk = session.GetInstance(Namespace, diskDrive);
    
  2. If necessary, view the properties of the object with a call to the Properties collection.

    Although not required, you may wish to know the value of the property before you change it.

    foreach (CimProperty property in myDisk.CimInstanceProperties)
    {
       Console.WriteLine(property.ToString());
    }
    
    Console.ReadLine();
    
  3. Make any changes to the local object properties.

    Doing so changes only the local copy. To save your changes to WMI, you must place the entire copy back into the WMI repository.

    myDisk.CimInstanceProperties["VolumeName"].Value = "NewName";
    
  4. Place the object back into the WMI repository using a call to CimSession.ModifyInstance.

    session.ModifyInstance(Namespace,myDisk);
    

The following procedure describes how to modify or update an instance using PowerShell.

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 modify or update an instance using C# (Microsoft.Management)

  1. Retrieve a local copy of the object with a call to ManagementObject.Get.

    using System.Management;
    ...
    ManagementObject myDisk = new ManagementObject("Win32_LogicalDisk.DeviceID='C:'");
    myDisk.Get();
    
  2. If necessary, view the properties of the object with a call to the Properties collection.

    Although not required, you may wish to know the value of the property before you change it.

    foreach (PropertyData property in myDisk.Properties)
    {
       Console.WriteLine(property.Name + " " + property.Value);
    }
    
    Console.ReadLine();
    
  3. Make any changes to the local object properties.

    Doing so changes only the local copy. To save your changes to WMI, you must place the entire copy back into the WMI repository.

    myDisk["VolumeName"] = "newName";
    
  4. Place the object back into the WMI repository using a call to the ManagementObject.Put or method.

    myDisk.Put();
    

The following procedure describes how to modify or update an instance using VBScript.

To modify or update an instance using VBScript

  1. Retrieve a local copy of the object with a call to GetObject.

  2. If necessary, view the properties of the object with a call to the Properties_ method.

    Although not required, you may wish to know the value of the property before you change it.

  3. Make any changes to the object properties with a call to the SWbemProperty.Value method.

    The Value method changes only the local copy. To save your changes to WMI, you must place the entire copy back into the WMI repository.

  4. Place the object back into the WMI repository with a call the SWbemObject.Put_ or SWbemObject.PutAsync_ methods.

As the names imply, Put_ updates synchronously while PutAsync_ updates asynchronously. Either method copies over the original instance with your modified instance. However, to take advantage of asynchronous processing, you must create an SWbemSink object. For more information, see Calling a method.

The following procedure describes how to modify or update an instance using C++.

To modify or update an instance using C++

  1. Retrieve a local copy of the instance with a call to IWbemServices::GetObject or IWbemServices::GetObjectAsync.

  2. If necessary, view the properties of the object with a call to IWbemClassObject::Get.

    Although not required, you may wish to know the value of the property before you change it.

  3. Make any necessary changes to the copy with a call to IWbemClassObject::Put.

    The Put method changes only the local copy. To save your changes to WMI, you must place the entire copy back into the WMI repository.

  4. Place your copy back into the WMI repository with a call the IWbemServices::PutInstance or IWbemServices::PutInstanceAsync methods.

    As the names imply, PutInstance updates synchronously while PutInstanceAsync updates asynchronously. Either method copies over the original instance with your modified instance. However, to take advantage of asynchronous processing, you must implement the IWbemObjectSink interface.

    You should be aware that an update operation on an instance belonging to a class hierarchy might not succeed due to an error involving another class in the hierarchy. WMI calls the PutInstanceAsync method of each of the providers responsible for the classes from which the class owning the original instance derives. If any of these providers fail, the original update request fails. For more information, see the Remarks section of PutInstanceAsync.

For more information, see Calling a Provider Method.

Note

Because the callback to the sink might not be returned at the same authentication level as the client requires, it is recommended that you use semisynchronous instead of asynchronous communication. For more information, see Calling a Method.