Share via


How to Configure a Advertisement to Allow Reboots Outside of a Maintenance Window

 

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

The following example shows how to configure an advertisement to allow reboots outside of a maintenance window by using the SMS_Advertisement class and the AdvertFlags class property in System Center 2012 R2 Configuration Manager.

To configure an advertisement to allow reboots outside of a maintenance window

  1. Set up a connection to the SMS Provider.

  2. Load an existing advertisement object using the SMS_Advertisement class.

  3. Modify the AdvertFlags property using the hexadecimal value for REBOOT_OUTSIDE_OF_MAINTENANCE_WINDOW.

  4. Save the modified advertisement and properties.

Example

The following example method configures an existing advertisement to allow reboots outside of a maintenance window.

Important

The hexadecimal values that define the AdvertFlags property are listed in the SMS_Advertisement reference material.

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

Sub ModifyAdvertisementToRebootOutsideOfMaintenanceWindows(connection, existingAdvertisementID)

    ' Define a constant with the hexadecimal value for the REBOOT_OUTSIDE_OF_MAINTENANCE_WINDOWS. 
    Const REBOOT_OUTSIDE_OF_MAINTENANCE_WINDOWS = &H00200000
    Dim advertisementToModify
    ' 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 "Advertisement Flags (integer):  " & advertisementToModify.AdvertFlags

    ' Set the new property value.
    advertisementToModify.AdvertFlags = advertisementToModify.AdvertFlags OR REBOOT_OUTSIDE_OF_MAINTENANCE_WINDOWS

    ' 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 "Advertisement Flags (integer):  " & advertisementToModify.AdvertFlags

End Sub
public void ModifySWDAdvertisementToRebootOutsideOfMaintenanceWindows(WqlConnectionManager connection, 
                                                                  string existingAdvertisementID)
{
    // Define a constant with the hexadecimal value for REBOOT_OUTSIDE_OF_MAINTENANCE_WINDOWS. 
    const Int32 REBOOT_OUTSIDE_OF_MAINTENANCE_WINDOWS = 0x00200000;

    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("Advertisement Flags (integer): " + advertisementToModify["AdvertFlags"].IntegerValue);

        // Modify the AdvertFlags value to include the REBOOT_OUTSIDE_OF_MAINTENANCE_WINDOWS value.
        advertisementToModify["AdvertFlags"].IntegerValue = advertisementToModify["AdvertFlags"].IntegerValue | REBOOT_OUTSIDE_OF_MAINTENANCE_WINDOWS;

        // Save the advertisement with the new value.
        advertisementToModify.Put();

        // Reload the advertisement to verify the change.
        advertisementToModify.Get();

        // List the existing (modified) property values.
        Console.WriteLine();
        Console.WriteLine("Values after change:");
        Console.WriteLine("_____________________");
        Console.WriteLine("Advertisement Name:            " + advertisementToModify["AdvertisementName"].StringValue);
        Console.WriteLine("Advertisement Flags (integer): " + advertisementToModify["AdvertFlags"].IntegerValue);
    }
    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.

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