Share via


How to Assign an Advertisement to a Collection of Computers

 

Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager

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

Note

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

To assign an advertisement to a collection

  1. Set up a connection to the SMS Provider.

  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)

    ' Get the specific advertisement object.
    Set advertisementToAssign = connection.Get("SMS_Advertisement.AdvertisementID='" & existingAdvertisementID & "'")

    ' Fill the advertisement properties for collection.
    advertisementToAssign.CollectionID = existingCollectionID

    ' 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)
{
    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;

        // 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.

Compiling the Code

The C# example requires:

Namespaces

System

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

mscorlib

Robust Programming

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

See Also

Configuration Manager Software Distribution
Software Distribution Advertisements
SMS_Collection Server WMI Class