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

To modify advertisement properties

  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 an existing advertisement ID.

  3. Replace the existing advertisement property (in this case, advertisement comment).

  4. Save the new advertisement and properties.

Example

The following example method modifies advertisement properties for software distribution.

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

Visual Basic Script  Copy Code
Sub ModifyAdvertisement(connection, existingAdvertisementID, newAdvertisementComment )

	' Get the specific advertisement instance to modify. 
	Set advertisementToModify = connection.Get("SMS_Advertisement.AdvertisementID='" & existingAdvertisementID & "'")

	' List the existing property values.
	Wscript.Echo " "
	Wscript.Echo "Values before change: "
	Wscript.Echo "--------------------- "
	Wscript.Echo "Advertisement Name: " & advertisementToModify.AdvertisementName
	Wscript.Echo "Comment:			" & advertisementToModify.Comment

	' Set the new property value.
	advertisementToModify.Comment = newAdvertisementComment

	' Save the advertisement.
	advertisementToModify.Put_ 

	' Output the new property values.
	Wscript.Echo " "
	Wscript.Echo "Values after change:  "
	Wscript.Echo "--------------------- "
	Wscript.Echo "Advertisement Name: " & AdvertisementToModify.AdvertisementName
	Wscript.Echo "Comment:			" & AdvertisementToModify.Comment

End Sub
C#  Copy Code
public void ModifySWDAdvertisement(WqlConnectionManager connection, string existingAdvertisementID, string newAdvertisementComment)
{
	try
	{
		// Get the specific advertisement instance to modify. 
		IResultObject advertisementToModify = connection.GetInstance(@"SMS_Advertisement.AdvertisementID='" + existingAdvertisementID + "'");

		// List the existing property values.
		Console.WriteLine();
		Console.WriteLine("Values before change:");
		Console.WriteLine("_____________________");
		Console.WriteLine("Advertisement Name: " + advertisementToModify["AdvertisementName"].StringValue);
		Console.WriteLine("Comment: " + advertisementToModify["Comment"].StringValue);

		// Set the new property value to  be modified.
		advertisementToModify["Comment"].StringValue = newAdvertisementComment;
	
		// Save the advertisement with the new value.
		advertisementToModify.Put();

		// Output the new property values.
		Console.WriteLine();
		Console.WriteLine("Values after change:");
		Console.WriteLine("____________________");
		Console.WriteLine("Advertisement Name: " + advertisementToModify["AdvertisementName"].StringValue);
		Console.WriteLine("Comment: " + newAdvertisementComment);
}
	catch (SmsException ex)
	{
		Console.WriteLine("Failed to modify 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.

existingAdvertisementID

  • Managed: String

  • VBScript: String

The ID of the advertisement to modify.

newAdvertisementComment

  • Managed: String

  • VBScript: String

The new comment for the advertisement.

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


Send comments about this topic to Microsoft.