다음을 통해 공유


How to Create an Advertisement

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

The following example shows how to create an advertisement by using the SMS_Advertisement Server WMI Class class and class properties in Microsoft System Center Configuration Manager 2007.

To create an advertisement

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

  2. Create the new advertisement object using the SMS_Advertisement Server WMI Class class.

  3. Populate the new advertisement properties.

  4. Save the new advertisement and properties.

Example

The following example method creates an advertisement for software distribution.

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

Sub SWDCreateAdvertisement(connection, existingCollectionID, existingPackageID, existingProgramName, newAdvertisementName, newAdvertisementComment, newAdvertisementFlags, newAdvertisementStartOfferDateTime, newAdvertisementStartOfferEnabled)

    ' Create the new advertisement object.
    Set newAdvertisement = connection.Get("SMS_Advertisement").SpawnInstance_
    
    ' Populate the advertisement properties.
    newAdvertisement.CollectionID = existingCollectionID
    newAdvertisement.PackageID = existingPackageID
    newAdvertisement.ProgramName = existingProgramName
    newAdvertisement.AdvertisementName = newAdvertisementName
    newAdvertisement.Comment = newAdvertisementComment
    newAdvertisement.AdvertFlags = newAdvertisementFlags
    newAdvertisement.PresentTime = newAdvertisementStartOfferDateTime
    newAdvertisement.PresentTimeEnabled = newAdvertisementStartOfferEnabled
    
    ' Save the new advertisement and properties.
    newAdvertisement.Put_ 
    
    ' Output new advertisement name.
    Wscript.Echo "Created advertisement: " & newAdvertisement.AdvertisementName

End Sub
public void CreateSWDAdvertisement(WqlConnectionManager connection, string existingCollectionID, string existingPackageID, string existingProgramName, string newAdvertisementName, string newAdvertisementComment, int newAdvertisementFlags, string newAdvertisementStartOfferDateTime, bool newAdvertisementStartOfferEnabled)
{
    try
    {
        // Create new advertisement instance.
        IResultObject newAdvertisement = connection.CreateInstance("SMS_Advertisement");

        // Populate new advertisement values.
        newAdvertisement["CollectionID"].StringValue = existingCollectionID;
        newAdvertisement["PackageID"].StringValue = existingPackageID;
        newAdvertisement["ProgramName"].StringValue = existingProgramName;
        newAdvertisement["AdvertisementName"].StringValue = newAdvertisementName;
        newAdvertisement["Comment"].StringValue = newAdvertisementComment;
        newAdvertisement["AdvertFlags"].IntegerValue = newAdvertisementFlags;
        newAdvertisement["PresentTime"].StringValue = newAdvertisementStartOfferDateTime;
        newAdvertisement["PresentTimeEnabled"].BooleanValue = newAdvertisementStartOfferEnabled;

        // Save the new advertisment and properties.
        newAdvertisement.Put();

        // Output new assignment name.
        Console.WriteLine("Created advertisement: " + newAdvertisement["AdvertisementName"].StringValue);
    }

    catch (SmsException ex)
    {
        Console.WriteLine("Failed to assign advertisement. Error: " + ex.Message);
        throw;
    }
}

The example method has the following parameters:

Parameter Type Description

connection

swbemServices

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

existingCollectionID

  • Managed: String

  • VBScript: String

The ID of an existing collection with which to associate the advertisement.

existingPackageID

  • Managed: String

  • VBScript: String

The ID of an existing package with which to associate the advertisement.

existingProgramName

  • Managed: String

  • VBScript: String

The name for the program associated with the advertisement.

newAdvertisementName

  • Managed: String

  • VBScript: String

The name for the new advertisement.

newAdvertisementComment

  • Managed: String

  • VBScript: String

A comment for the new advertisement.

newAdvertisementFlags

  • Managed: Integer

  • VBScript: Integer

Flags specifying options for the new advertisement.

newAdvertisementStartOfferDateTime

  • Managed: String

  • VBScript: String

The time when the new advertisement is first offered.

newAdvertisementStartOfferEnabled

  • Managed: Boolean

  • VBScript: Boolean

true if the advertisement is offered.

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

Concepts

Configuration Manager Software Distribution
Software Distribution Advertisements
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