Share via


How to Call a WMI Class Method by Using System.Management

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

To call a client Windows Management Instrumentation (WMI) class method, in Microsoft System Center Configuration Manager 2007, you call the InvokeMethod of the WMI class's ManagementClass.

To call a WMI class method

  1. Set up a connection to the Configuration Manager 2007 client WMI namespace. For more information, see How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management.

  2. Create a ManagementClass by using the ManagementScope path you obtain in step one, and also the name of the class you want to call a method on.

  3. Create a ManagementBaseObject and specify any in parameters for the method.

  4. Call the method by using the ManagementClass object InvokeMethod method.

  5. Using the returned ManagementBaseObject, view the returned parameters.

Example

The following C# code example calls the ISmsClient::GetAssignedSite Method 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 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

Concepts

ISmsClient::GetAssignedSite Method
ISmsClient::SetAssignedSite Method
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