Редактиране

Споделяне чрез


How to Create an Association Between Two Computers in Configuration Manager

You create an association between a reference and destination computer, in Configuration Manager, by calling the AddAssociation Method in Class SMS_StateMigration.

Note

You call the DeleteAssociation Method in Class SMS_StateMigration to delete an association.

To create an association between two computers

  1. Set up a connection to the SMS Provider. For more information, see SMS Provider fundamentals.

  2. Call the AddAssociation Method in Class SMS_StateMigration.

Example

The following example method adds an association between a source and reference computer.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

Sub AssociateComputer(connection, referenceComputerResourceId, destinationComputerResourceId)  

    Dim stateMigrationClass  
    Dim inParams  
    Dim outParams  

    ' Get the state migration class.  
    Set stateMigrationClass = connection.Get("SMS_StateMigration")  

    ' Set up the parameters.  
    Set inParams = _  
      stateMigrationClass.Methods_("AddAssociation").InParameters.SpawnInstance_  
    inParams.SourceClientResourceID = referenceComputerResourceId  
    inParams.RestoreClientResourceID = destinationComputerResourceId  

    ' Call the method.  
    Set outParams = _  
      connection.ExecMethod( "SMS_StateMigration", "AddAssociation", inParams)     

   End Sub  
public void AssociateComputer(  
    WqlConnectionManager connection,   
    int referenceComputerResourceId,   
    int destinationComputerResourceId)  
{  
    try  
    {  
        // Set up the reference and destination computer in parameters.  
        Dictionary<string, object> inParams = new Dictionary<string, object>();  
        inParams.Add("SourceClientResourceID", referenceComputerResourceId);  
        inParams.Add("RestoreClientResourceID", destinationComputerResourceId);  

        // Create the computer association.  
       connection.ExecuteMethod("SMS_StateMigration", "AddAssociation", inParams);  
    }  
    catch (SmsException e)  
    {  
        Console.WriteLine("failed to make the association" + e.Message);  
        throw;  
    }  
}  

The example method has the following parameters:

Parameter Type Description
connection - Managed: WqlConnectionManager
- VBScript: SWbemServices
A valid connection to the SMS Provider.
referenceComputerResourceID - Managed: Integer
- VBScript: Integer
The Configuration Manager resource identifier for the reference computer. This is available from SMS_R_System class ResourceId property for the computer.
destinationComputerResourceID - Managed: Integer
- VBScript: Integer
The Configuration Manager resource identifier for the destination computer. This is available from SMS_R_System class ResourceId property for the computer.

Compiling the Code

The C# example has the following compilation requirements:

Namespaces

System

System.Collections.Generic

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

.NET Framework Security

For more information about securing Configuration Manager applications, see Configuration Manager role-based administration.

See Also

About OS deployment computer management AddAssociation Method in Class SMS_StateMigration
DeleteAssociation Method in Class SMS_StateMigration