다음을 통해 공유


How to Assign an Advertisement to a Collection of Computers

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

You can assign an advertisement to a collection by using the SMS_Advertisement Server WMI Class class in Microsoft System Center Configuration Manager 2007. Advertisements are closely tied to packages, programs and collections. For more information, see Software Distribution Overview.

Note

Detailed information about the SMS_Advertisement Server WMI Class class and class properties is in the reference section of the System Center Configuration Manager 2007 Software Development Kit (SDK).

To assign an advertisement to a collection

  1. Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.

  2. Get the specific advertisement using the existing advertisement ID.

  3. Populate the advertisement collection ID property with the existing collection ID.

  4. Save the advertisement and properties.

Example

The following example method assigns a specific advertisement to a collection for use in software distribution.

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

Sub SWDAssignAdvertisementCollection(connection, existingAdvertisementID, existingCollectionID, includeSubCollection)

    ' Get the specific advertisement object.
    Set advertisementToAssign = connection.Get("SMS_Advertisement.AdvertisementID='" & existingAdvertisementID & "'")
    
    ' Fill the advertisement properties for collection.
    advertisementToAssign.CollectionID = existingCollectionID
    advertisementToAssign.IncludeSubCollection = IncludeSubCollection
    
    ' Save the advertisement.
    advertisementToAssign.Put_
    
    ' Output advertisement and collection information.
    Wscript.Echo "Assigned advertisement: " & existingAdvertisementID
    Wscript.Echo "                        " & advertisementToAssign.AdvertisementName
    Wscript.Echo "To collection:          " & advertisementToAssign.CollectionID

End Sub
public void AssignSWDAdvertisementToCollection(WqlConnectionManager connection, string existingAdvertisementID, string existingCollectionID, bool includeSubCollection)
{
    try
    {
        // Get specific advertisement instance (using the passed in value existingAdvertisementID).
        IResultObject advertisementToAssign = connection.GetInstance(@"SMS_Advertisement.AdvertisementID='" + existingAdvertisementID + "'");

        // Populate the collection id property of the advertisement.
        advertisementToAssign["CollectionID"].StringValue = existingCollectionID;
        advertisementToAssign["IncludeSubCollection"].BooleanValue = includeSubCollection;
        
        // Save the advertisment and properties.
        advertisementToAssign.Put();

        // Output advertisment and collection information.
        Console.WriteLine("Assigned advertisement: " + existingAdvertisementID);
        Console.WriteLine("                        " + advertisementToAssign["AdvertisementName"].StringValue);
        Console.WriteLine("To collection:          " + existingCollectionID);
    }
    catch (SmsException ex)
    {
        Console.WriteLine("Failed to assign advertisement. Error: " + ex.Message);
        throw;
    }
}

The example method has the following parameters:

Parameter Type Description

connection

swebemServices

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

existingAdvertisementID

  • Managed: String

  • VBScript: String

The ID of an existing advertisement.

existingCollectionID

  • Managed: String

  • VBScript: String

The ID of an existing collection.

includeSubCollection

  • Managed: Boolean

  • VBScript: Boolean

true to include subcollections.

Compiling the Code

The C# example requires:

Namespaces

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

Robust Programming

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

Security

For more information about securing Configuration Manager applications, see Securing Configuration Manager Applications.

See Also

Reference

SMS_Collection Server WMI Class

Concepts

Configuration Manager Software Distribution
Software Distribution Advertisements
Configuration Manager Collections
How to Use Configuration Manager Objects with WMI
How to Use Configuration Manager Objects with Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using WMI
SMS_Advertisement Server WMI Class