Compartilhar via


Como configurar um anúncio para substituir uma janela de manutenção

O exemplo a seguir mostra como configurar um anúncio para substituir janelas de serviço usando a SMS_Advertisement classe e a propriedade de AdvertFlags classe em Configuration Manager.

Para configurar um anúncio para substituir janelas de manutenção

  1. Configure uma conexão com o Provedor de SMS.

  2. Carregue um objeto de anúncio existente usando a SMS_Advertisement classe.

  3. Modifique a AdvertFlags propriedade usando o valor hexadecimal para OVERRIDE_MAINTENANCE_WINDOW.

  4. Salve o anúncio e as propriedades modificadas.

Exemplo

O método de exemplo a seguir configura um anúncio existente para substituir janelas de manutenção.

Importante

Os valores hexadecimal que definem a AdvertFlags propriedade são listados no material de referência da SMS_Advertisement classe.

Para obter informações sobre como chamar o código de exemplo, consulte Chamando Configuration Manager Snippets de Código.


Sub ModifyAdvertisementToOverrideMaintenanceWindows(connection, existingAdvertisementID)

    ' Define a constant with the hexadecimal value for the OVERRIDE_MAINTENANCE_WINDOW.
    Const OVERRIDE_MAINTENANCE_WINDOWS = &H00100000
    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 OVERRIDE_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 ModifySWDAdvertisementToOverrideMaintenanceWindows(WqlConnectionManager connection,
                                                               string existingAdvertisementID)
{
    // Define a constant with the hexadecimal value for OVERRIDE_MAINTENANCE_WINDOW.
    const Int32 OVERRIDE_MAINTENANCE_WINDOWS = 0x00100000;

    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 OVERRIDE_MAINTENANCE_WINDOWS value.
        advertisementToModify["AdvertFlags"].IntegerValue = advertisementToModify["AdvertFlags"].IntegerValue | OVERRIDE_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;
    }
}

O método de exemplo tem os seguintes parâmetros:

Parâmetro Tipo Descrição
connection

swbemServices
-Gerenciado: WqlConnectionManager
- VBScript: SWbemServices
Uma conexão válida com o provedor de SMS.
existingAdvertisementID -Gerenciado: String
-Vbscript: String
A ID do anúncio a ser modificado.

Compilando o código

O exemplo C# requer:

Namespaces

Sistema

Microsoft. ConfigurationManagement.ManagementProvider

Microsoft. ConfigurationManagement.ManagementProvider.WqlQueryEngine

Mscorlib

Assembly

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

Programação robusta

Para obter mais informações sobre o tratamento de erros, consulte Sobre erros de Configuration Manager.

Confira também

Visão geral da distribuição de softwaresobre implantações