适用于:✔️ Windows VM ✔️ Linux VM ✔️ 本地环境 ✔️ 已启用 Azure Arc 的服务器 ✔️ Azure VM。
前置事件和后置事件允许你在计划的维护配置前后执行用户定义的操作。 有关详细信息,请参阅 Azure 更新管理器中前置事件和后置事件的工作。
本文介绍了如何在 Azure 更新管理器中创建预维护和后期维护事件。
计划维护配置中的事件网格
Azure 更新管理器利用事件网格创建和管理预维护和后期维护事件。 有关详细信息,请参阅事件网格概述。 若要在计划维护时段之前或之后触发某个事件,需要满足以下条件:
- 
              
              计划维护配置 - 可以在 Azure 更新管理器中为计划维护配置创建前置事件和后置事件。
- 
              
              要在预维护和后期维护事件中执行的操作 - 可以使用事件网格支持的事件处理程序(终结点)来定义操作或任务。 下面是有关如何通过 Webhook 和 Azure Functions 创建 Azure 自动化 Runbook 的示例。 在这些事件处理程序/终结点中,你必须定义应当在前置事件和后置事件中执行的操作。
- 
              
              Webhook - 创建 PowerShell 7.2 Runbook 并将 Runbook 链接到 Webhook。
- 
              
              Azure 函数 - 创建 Azure 函数。
 
- 
              
              前置事件和后置事件 - 你可以按照以下部分中分享的步骤为计划维护配置创建前置事件和后置事件。 若要详细了解事件网格的“基本信息”选项卡中使用的术语,请参阅事件网格术语。
创建预维护和后期维护事件
在创建新的计划维护配置时创建预维护和后期维护事件
- 登录到 Azure 门户,然后转到“Azure 更新管理器”。 
- 在“管理”下,选择“计算机”。 
- 从顶部的功能区中选择“计划更新”。 
- 在“创建维护配置”页中,选择“事件”选项卡。 
- 选择“+事件订阅”以创建预维护/后期维护事件。 
- 在“添加事件订阅”页上,输入以下详细信息:在“事件订阅详细信息”部分中,提供适当的名称。 - 
- 将架构保留为“事件网格架构”。
- 输入在此维护配置中创建的第一个事件的“系统主题名称”。 系统将自动为后续事件填充此系统主题名称。
- 在“事件类型”部分,在“筛选事件类型”下选择要推送到终结点或目标的事件类型。 可以选择“预期维护事件”和/或“后期维护事件”。
- 在“终结点详细信息”部分,选择要从中接收响应的终结点。
 
- 选择“添加”以在创建计划时为其创建预维护和后期维护事件。 - 
              
              
                
 
注意
在上述流中,Webhook 和 Azure Functions 是可从中选择的两个事件处理程序/终结点。 选择“添加”时,不会创建事件订阅,而是会将其添加到维护配置。 事件订阅与计划维护配置一起创建。
 
- 按照此处列出的步骤创建维护配置。 
- 
 # Obtain the Maintenance Configuration ID from Step 1 and assign it to MaintenanceConfigurationResourceId variable  
 $MaintenanceConfigurationResourceId = "/subscriptions/<subId>/resourceGroups/<Resource group>/providers/Microsoft.Maintenance/maintenanceConfigurations/<Maintenance configuration Name>"
 # Use the same Resource Group that you used to create maintenance configuration in Step 1
 $ResourceGroupForSystemTopic = "<Resource Group for System Topic>"
 $SystemTopicName = "<System topic name>"
 $TopicType = "Microsoft.Maintenance.MaintenanceConfigurations"
 $SystemTopicLocation = "<System topic location>"
 # System topic creation
 New-AzEventGridSystemTopic -ResourceGroupName $ResourceGroupForSystemTopic -Name $SystemTopicName -Source $MaintenanceConfigurationResourceId -TopicType $TopicType -Location $SystemTopicLocation
 # Event subscription creation
 $IncludedEventTypes = @("Microsoft.Maintenance.PreMaintenanceEvent")
 # Webhook
 $EventSubscriptionName = "PreEventWebhook"
 $PreEventWebhookEndpoint = "<Webhook URL>"
 $dest = New-AzEventGridWebHookEventSubscriptionDestinationObject -EndpointUrl $PreEventWebhookEndpoint
 New-AzEventGridSystemTopicEventSubscription -ResourceGroupName $ResourceGroupForSystemTopic -SystemTopicName $SystemTopicName -EventSubscriptionName $EventSubscriptionName -Endpoint $PreEventWebhookEndpoint -IncludedEventType $IncludedEventTypes -Destination $dest
 # Azure Function
 $dest = New-AzEventGridAzureFunctionEventSubscriptionDestinationObject -ResourceId "<Azure Function Resource Id>"
 New-AzEventGridSystemTopicEventSubscription -ResourceGroupName $ResourceGroupForSystemTopic -SystemTopicName $SystemTopicName -EventSubscriptionName $EventSubscriptionName -Destination $dest -IncludedEventType $IncludedEventTypes
 
- 按照此处列出的步骤创建维护配置。 
- 
 SystemTopicName="<System topic name> 
 # Use the same Resource Group that you used to create maintenance configuration in Step 1
 ResourceGroupName="<Resource Group mentioned in Step 1>"
 # Obtain the Maintenance Configuration ID from Step 1 and assign it to Source variable
 Source="/subscriptions/<subId>/resourceGroups/<Resource group>/providers/Microsoft.Maintenance/maintenanceConfigurations/<Maintenance configuration Name>"
 TopicType="Microsoft.Maintenance.MaintenanceConfigurations"
 Location="<System topic location> "
 # System topic creation
 az eventgrid system-topic create --name $SystemTopicName --resource-group $ResourceGroupName --source $Source --topic-type $TopicType --location $Location
 # Event subscription creation
 IncludedEventTypes='("Microsoft.Maintenance.PreMaintenanceEvent")'
 # Webhook
 az eventgrid system-topic event-subscription create --name "<Event subscription name>" --resource-group $ResourceGroupName --system-topic-name $SystemTopicName --endpoint-type webhook --endpoint "<webhook URL>" --included-event-types IncludedEventTypes
 # Azure Function
 az eventgrid system-topic event-subscription create –name "<Event subscription name>" --resource-group $ResourceGroupName --system-topic-name $SystemTopicName --endpoint-type azurefunction --endpoint "<Azure Function ResourceId>" --included-event-types IncludedEventTypes
 
- 按照此处列出的步骤创建维护配置。 
- # 系统主题创建了解详细信息 - PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>?api-version=2022-06-15
 - 请求正文: - {
  "properties": {
    "source": "/subscriptions/<subscription Id>/resourceGroups/<resource group>/providers/Microsoft.Maintenance/maintenanceConfigurations/<maintenance configuration name> ",
    "topicType": "Microsoft.Maintenance.MaintenanceConfigurations"
  },
  "location": "<location>"
}
 - # 事件订阅创建了解详细信息 - 允许的事件类型 - Microsoft.Maintenance.PreMaintenanceEvent、Microsoft.Maintenance.PostMaintenanceEvent - 
              Webhook - PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>/eventSubscriptions/<Event Subscription name>?api-version=2022-06-15
 - 请求正文: - {
  "properties": {
    "destination": {
      "endpointType": "WebHook",
      "properties": {
        "endpointUrl": "<Webhook URL>"
      }
    },
    "filter": {
      "includedEventTypes": [
        "Microsoft.Maintenance.PreMaintenanceEvent"
      ]
    }
  }
}
 - 
              Azure 函数 - PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>/eventSubscriptions/<Event Subscription name>?api-version=2022-06-15
 - 
              请求正文 - {
    "properties": {
        "destination": {
            "endpointType": "AzureFunction",
            "properties": {
                "resourceId": "<Azure Function Resource Id>"
            }
        }
    },
    "filter": {
        "includedEventTypes": [
            "Microsoft.Maintenance.PostMaintenanceEvent"
        ]
    }
}
 
 
 
 
在现有计划维护配置上创建预维护和后期维护事件
- 登录到 Azure 门户,然后转到“Azure 更新管理器”。 
- 在“管理”下,选择“计算机”、“维护配置”。 
- 在“维护配置”页上,选择要向其添加预维护和后期维护事件的维护配置。 
- 在所选“维护配置”页上的“设置”下,选择“事件”。 或者,在“概述”下,选择卡片“创建维护事件”。 - 
              
              
                
 
- 选择“+事件订阅”以创建预维护/后期维护事件。 - 
              
              
                
 
- 在“创建事件订阅”页上,输入以下详细信息: - 
- 在“事件订阅详细信息”部分,提供适当的名称。 
- 将架构保留为“事件网格架构”。 
- 在“主题详细信息”部分,为“系统主题名称”提供适当的名称。 
- 在“事件类型”部分,在“筛选事件类型”下选择要推送到终结点或目标的事件类型。 可以在预维护事件与后期维护事件之间选择。 
- 在“终结点详细信息”部分,选择要从中接收响应的终结点。 - 
              
                
 
 
- 选择“创建”以配置现有计划中的预维护和后期维护事件。 
    $MaintenanceConfigurationResourceId = "/subscriptions/<subId>/resourceGroups/<Resource group>/providers/Microsoft.Maintenance/maintenanceConfigurations/<Maintenance configuration Name>"
    $ResourceGroupForSystemTopic = "<Resource Group for System Topic>"
    $SystemTopicName = "<System topic name>"
    $TopicType = "Microsoft.Maintenance.MaintenanceConfigurations"
    $SystemTopicLocation = "<System topic location>"
    # System topic creation
    New-AzEventGridSystemTopic -ResourceGroupName $ResourceGroupForSystemTopic -Name $SystemTopicName -Source $MaintenanceConfigurationResourceId -TopicType $TopicType -Location $SystemTopicLocation
    # Event subscription creation
    $IncludedEventTypes = @("Microsoft.Maintenance.PreMaintenanceEvent")
    # Webhook
    $EventSubscriptionName = "PreEventWebhook"
    $PreEventWebhookEndpoint = "<Webhook URL>"
    $dest = New-AzEventGridWebHookEventSubscriptionDestinationObject -EndpointUrl $PreEventWebhookEndpoint
    New-AzEventGridSystemTopicEventSubscription -ResourceGroupName $ResourceGroupForSystemTopic -SystemTopicName $SystemTopicName -EventSubscriptionName $EventSubscriptionName -Endpoint $PreEventWebhookEndpoint -IncludedEventType $IncludedEventTypes -Destination $dest
    # Azure Function
    $dest = New-AzEventGridAzureFunctionEventSubscriptionDestinationObject -ResourceId "<Azure Function Resource Id>"
    New-AzEventGridSystemTopicEventSubscription -ResourceGroupName $ResourceGroupForSystemTopic -SystemTopicName $SystemTopicName -EventSubscriptionName $EventSubscriptionName -Destination $dest -IncludedEventType $IncludedEventTypes
   SystemTopicName="<System topic name> 
   ResourceGroupName="<Resource Group for System Topic>"
   Source="/subscriptions/<subId>/resourceGroups/<Resource group>/providers/Microsoft.Maintenance/maintenanceConfigurations/<Maintenance configuration Name>"
    TopicType="Microsoft.Maintenance.MaintenanceConfigurations"
    Location="<System topic location> "
   # System topic creation
    az eventgrid system-topic create --name $SystemTopicName --resource-group $ResourceGroupName --source $Source --topic-type $TopicType --location $Location
   # Event subscription creation
    IncludedEventTypes='("Microsoft.Maintenance.PreMaintenanceEvent")'
   # Webhook
    az eventgrid system-topic event-subscription create --name "<Event subscription name>" --resource-group $ResourceGroupName --system-topic-name $SystemTopicName --endpoint-type webhook --endpoint "<webhook URL>" --included-event-types IncludedEventTypes
   # Azure Function
    az eventgrid system-topic event-subscription create –name "<Event subscription name>" --resource-group $ResourceGroupName --system-topic-name $SystemTopicName --endpoint-type azurefunction --endpoint "<Azure Function ResourceId>" --included-event-types IncludedEventTypes
# 系统主题创建了解详细信息
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>?api-version=2022-06-15
请求正文:
{
  "properties": {
    "source": "/subscriptions/<subscription Id>/resourceGroups/<resource group>/providers/Microsoft.Maintenance/maintenanceConfigurations/<maintenance configuration name> ",
    "topicType": "Microsoft.Maintenance.MaintenanceConfigurations"
  },
  "location": "<location>"
}
# 事件订阅创建了解详细信息
允许的事件类型 - Microsoft.Maintenance.PreMaintenanceEvent、Microsoft.Maintenance.PostMaintenanceEvent
              Webhook
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>/eventSubscriptions/<Event Subscription name>?api-version=2022-06-15
请求正文:
{
  "properties": {
    "destination": {
      "endpointType": "WebHook",
      "properties": {
        "endpointUrl": "<Webhook URL>"
      }
    },
    "filter": {
      "includedEventTypes": [
        "Microsoft.Maintenance.PreMaintenanceEvent"
      ]
    }
  }
}
              Azure 函数
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>/eventSubscriptions/<Event Subscription name>?api-version=2022-06-15
              请求正文
{
    "properties": {
        "destination": {
            "endpointType": "AzureFunction",
            "properties": {
                "resourceId": "<Azure Function Resource Id>"
            }
        }
    },
    "filter": {
        "includedEventTypes": [
            "Microsoft.Maintenance.PostMaintenanceEvent"
        ]
    }
}
 
 
后续步骤
- 有关 Azure 更新管理器中的预维护和后期维护事件的概述,请参阅此文。
- 若要了解如何管理预维护和后期维护事件或取消计划运行,请参阅预维护和后期维护配置事件。
- 若要了解如何使用预维护和后期维护事件来通过 Webhook 打开和关闭 VM,请参阅此处。
- 若要了解如何使用预维护和后期维护事件来通过 Azure Functions 打开和关闭 VM,请参阅此处。