Udostępnij za pośrednictwem


How to Call Configuration Manager COM Automation Objects

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

Microsoft System Center Configuration Manager 2007 has several COM automation objects that you can easily call from VBScript and managed code.

VBScript

In VBScript you call the Configuration Manager 2007 COM automation objects as you would any other COM automation object. For more information, see https://go.microsoft.com/fwlink/?LinkId=110497.

The following procedure demonstrates how to create an SmsClient Client COM Automation Class object and then call a method to display the current assigned site for a client. Code similar to this can be used with the code snippets in this Configuration Manager SDK.

To call a COM automation object by using VBScript

  1. Open Notepad.exe.

  2. Enter the following code:

    Option Explicit
    On Error Resume Next
    
    Call GetAssignedSite
    
    WScript.Echo "Finished"
    
    Sub GetAssignedSite
    
        Dim smsClient
    
        Set smsClient = CreateObject ("Microsoft.SMS.Client")
    
        If Err.Number <> 0 then 
            WScript.Echo "Could not create SMS Client Object - quitting"
            Exit Sub
        End If
    
        WScript.Echo "Assigned Site is : " & smsClient.GetAssignedSite 
    End Sub
    
  3. Save the file with the name script.vbs. Be sure to save the file as type All Files.

  4. From the command prompt, navigate to the folder you saved the script.vbs in.

  5. Type cscript script.vbs and press ENTER. The current management point for the client should be displayed.

Managed Code

To use the Microsoft System Center Configuration Manager 2007 COM Automation objects with managed code, you use COM Interop. You use COM Interop in your Visual Studio project by first adding a reference to the COM Automation object and then by declaring and calling the COM automation object in your code.

For more information about COM Interop, see COM Interop Tutorials (https://go.microsoft.com/fwlink/?LinkId=111724).

The following procedure demonstrates how to add a reference to the SmsClient Client COM Automation Class automation object. It shows how to declare the object in code and then call a method, to get the current management point. Code similar to this can be used with the samples in this section.

Note

In the topics that use COM Automation objects, the required namespaces and assemblies are listed in the Compiling the Code Section.

To call a COM Automation object by using managed code

  1. Create a Visual Studio C# Console Application project.

  2. Add the SmsClientClass assembly as a reference to the project. Right-click the project, click Add Reference, click the COM tab of the Add Reference dialog box. Double-click SmsClientLib, and then press OK.

  3. Add the following code to the list of namespaces at the top of Program.cs.

    using System.Runtime.InteropServices;
    using SmsClientLib;
    
  4. Add the following code to the Main function in Program.cs.

    try
    {
        SmsClientClass client = new SmsClientClass();
    
        Console.WriteLine("Current management point is: " +
            client.GetCurrentManagementPoint());
    
    }
    catch (COMException e)
    {
        Console.WriteLine("Failed to set the management point " + e.Message);
        throw;
    }
    
  5. Build and run the project. The current management point for the client should be displayed.

See Also

Concepts

Configuration Manager Client Automation
Configuration Manager Client Control Panel Configuration
Software Distribution Control Panel Automation
Configuration Manager Client Configuration