How to Call a WMI Class Method by Using System.Management
To call a client Windows Management Instrumentation (WMI) class method, in Configuration Manager, you call the InvokeMethod
of the WMI class's ManagementClass
.
To call a WMI class method
Set up a connection to the Configuration Manager client WMI namespace. For more information, see How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management.
Create a
ManagementClass
by using theManagementScope
path you obtain in step one, and also the name of the class you want to call a method on.Create a
ManagementBaseObject
and specify any in parameters for the method.Call the method by using the
ManagementClass
objectInvokeMethod
method.Using the returned
ManagementBaseObject
, view the returned parameters.
Example
The following C# code example calls the ISmsClient::GetAssignedSite
method to get the current assigned site for the client. It then sets the assigned site back to the same value using the ISmsClient::SetAssignedSite
method.
For information about calling the sample code, see How to Call a WMI Class Method by Using System.Management.
public void CallMethod(ManagementScope scope)
{
try// Get the client's SMS_Client class.
{
ManagementClass cls = new ManagementClass(scope.Path.Path, "sms_client", null);
// Get current site code.
ManagementBaseObject outSiteParams = cls.InvokeMethod("GetAssignedSite", null, null);
// Display current site code.
Console.WriteLine(outSiteParams["sSiteCode"].ToString());
// Set up current site code as input parameter for SetAssignedSite.
ManagementBaseObject inParams = cls.GetMethodParameters("SetAssignedSite");
inParams["sSiteCode"] = outSiteParams["sSiteCode"].ToString();
// Assign the Site code.
ManagementBaseObject outMPParams = cls.InvokeMethod("SetAssignedSite", inParams, null);
}
catch (ManagementException e)
{
throw new Exception("Failed to execute method", e);
}
}
This example method has the following parameters:
Parameter | Type | Description |
---|---|---|
scope |
- ManagementScope |
A valid connection to the client WMI provider. The path is root\ccm. |
Compiling the Code
Namespaces
System
System.Management
Assembly
System.Management
Robust Programming
The exception that can be raised is System.Management.ManagementException.
See Also
About Configuration Manager WMI Programming
How to Call a WMI Class Method by Using System.Management
How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management
How to Perform an Asynchronous Query by Using System.Management
How to Perform a Synchronous Query by Using System.Management
How to Read a WMI Object by Using System.Management