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
Retrieve a local copy of the object with a call to Get-WmiObject.
$mySettings = get-WMIObject Win32_WmiSetting
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
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
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)
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);
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();
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";
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)
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();
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();
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";
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
Retrieve a local copy of the object with a call to GetObject.
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.
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.
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++
Retrieve a local copy of the instance with a call to IWbemServices::GetObject or IWbemServices::GetObjectAsync.
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.
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.
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.