The following example shows how to create an advertisement by using the SMS_Advertisement class and class properties in System Center 2012 Configuration Manager.
Important |
---|
To create an advertisement that targets a collection, you must have “Deploy Packages” permissions for the collection and “Read” permissions for the package. |
To create an advertisement
-
Set up a connection to the SMS Provider.
-
Create the new advertisement object using the SMS_Advertisement class.
-
Populate the new advertisement properties.
-
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.
Visual Basic Script | Copy Code |
---|---|
Sub SWDCreateAdvertisement(connection, existingCollectionID, existingPackageID, existingProgramName, newAdvertisementName, newAdvertisementComment, newAdvertisementFlags, newAdvertisementStartOfferDateTime, newAdvertisementStartOfferEnabled) Dim newAdvertisement ' 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 |
C# | Copy Code |
---|---|
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 |
|
A valid connection to the SMS Provider. |
|
existingCollectionID |
|
The ID of an existing collection with which to associate the advertisement. |
|
existingPackageID |
|
The ID of an existing package with which to associate the advertisement. |
|
existingProgramName |
|
The name for the program associated with the advertisement. |
|
newAdvertisementName |
|
The name for the new advertisement. |
|
newAdvertisementComment |
|
A comment for the new advertisement. |
|
newAdvertisementFlags |
|
Flags specifying options for the new advertisement. |
|
newAdvertisementStartOfferDateTime |
|
The time when the new advertisement is first offered. |
|
newAdvertisementStartOfferEnabled |
|
true if the advertisement is offered. |
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.