您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:将 Azure 自动化与事件网格和 Microsoft Teams 相集成Tutorial: Integrate Azure Automation with Event Grid and Microsoft Teams

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 导入事件网格示例 runbook。Import an Event Grid sample runbook.
  • 创建可选 Microsoft Teams Webhook。Create an optional Microsoft Teams webhook.
  • 为 runbook 创建 Webhook。Create a webhook for the runbook.
  • 创建事件网格订阅。Create an Event Grid subscription.
  • 创建触发 runbook 的 VM。Create a VM that triggers the runbook.

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

重要

使用 PowerShell 的 Azure 功能必须已安装 AzureRM 模块。Using this Azure feature from PowerShell requires that you have the AzureRM module installed. 该模块是旧模块,仅适用于 Windows PowerShell 5.x,它只接收关键更新,但不再获取新功能。This is an older module only available for Windows PowerShell 5.x that receives critical updates, but no longer gets new features. 如果已安装 Az 模块,则还需要安装 AzureRMIf you have the Az module installed, you will need to install AzureRM as well. 安装 AzureRMTo install AzureRM:

  1. 使用 Disable-AzAlias 禁用 Az(若已启用)中的 AzureRM 别名。Disable AzureRM aliases in Az (if enabled) with Disable-AzAlias. 在没有安装 Az 或禁用别名的情况下,也可以运行此命令。Running this command without Az installed or with aliases disabled is harmless.
  2. 请遵循 AzureRM 的最新安装说明Follow the latest install instructions for AzureRM. 即使已安装 AzureRM,也请将其更新到最新版本。Even if you have AzureRM installed, update to the latest version.

若要完成此教程,需要 Azure 自动化帐户,以保存从 Azure 事件网格订阅触发的 runbook。To complete this tutorial, an Azure Automation account is required to hold the runbook that is triggered from the Azure Event Grid subscription.

导入事件网格示例 runbookImport an Event Grid sample runbook

  1. 选择自动化帐户并选择 Runbook 页。Select your Automation account, and select the Runbooks page.

    选择 runbook

  2. 选择“浏览库”按钮。Select the Browse gallery button.

  3. 搜索“事件网格”并选择“集成 Azure 自动化与事件网格”。Search for Event Grid, and select Integrating Azure Automation with Event grid.

    导入库 runbook

  4. 选择“导入”并将其命名为 Watch-VMWrite。Select Import and name it Watch-VMWrite.

  5. 导入后,选择“编辑”查看 runbook 源。After it has imported, select Edit to view the runbook source. 选择“发布”按钮。Select the Publish button.

备注

需要将脚本中的第 74 行更改为 Update-AzureRmVM -ResourceGroupName $VMResourceGroup -VM $VM -Tag $Tag | Write-VerboseLine 74 in the script needs to have the line changed to Update-AzureRmVM -ResourceGroupName $VMResourceGroup -VM $VM -Tag $Tag | Write-Verbose. -Tags 参数现在是 -TagThe -Tags parameter is now -Tag.

创建可选 Microsoft Teams WebhookCreate an optional Microsoft Teams webhook

  1. 在 Microsoft Teams 中,选择通道名称旁的“更多选项”,然后选择“连接器”。In Microsoft Teams, select More Options next to the channel name, and then select Connectors.

    Microsoft Teams 连接

  2. 在连接器列表中滚动到“传入 Webhook”,并选择“添加”。Scroll through the list of connectors to Incoming Webhook, and select Add.

  3. 输入 AzureAutomationIntegration 作为名称,并选择“创建”。Enter AzureAutomationIntegration for the name, and select Create.

  4. 将 Webhook 复制到剪贴板并保存。Copy the webhook to the clipboard, and save it. Webhook URL 用于将信息发送到 Microsoft Teams。The webhook URL is used to send information to Microsoft Teams.

  5. 选择“完成”保存 Webhook。Select Done to save the webhook.

为 runbook 创建 WebhookCreate a webhook for the runbook

  1. 打开 Watch-VMWrite runbook。Open the Watch-VMWrite runbook.

  2. 选择 Webhook 并选择“添加 Webhook”按钮。Select Webhooks, and select the Add Webhook button.

  3. 输入 WatchVMEventGrid 作为名称。Enter WatchVMEventGrid for the name. 将 URL 复制到剪贴板并保存。Copy the URL to the clipboard, and save it.

    配置 Webhook 名称

  4. 选择“配置参数并运行设置”,然后输入 Microsoft Teams Webhook URL 作为 CHANNELURL。Select Configure parameters and run settings, and enter the Microsoft Teams webhook URL for CHANNELURL. 将 WEBHOOKDATA 保留空白。Leave WEBHOOKDATA blank.

    配置 Webhook 参数

  5. 选择“创建”创建自动化 runbook Webhook。Select Create to create the Automation runbook webhook.

创建事件网格订阅Create an Event Grid subscription

  1. 在“自动化帐户”概述页上,选择“事件网格”。On the Automation Account overview page, select Event grid.

    选择“事件网格”

  2. 单击“+ 事件订阅”。Click + Event Subscription.

  3. 使用以下信息配置订阅:Configure the subscription with the following information:

    • 对于“主题类型”,选择“Azure 订阅”。For Topic Type, select Azure Subscriptions.
    • 取消选中“订阅所有事件类型”复选框。Uncheck the Subscribe to all event types check box.
    • 输入 AzureAutomation 作为名称。Enter AzureAutomation for the name.
    • 在“定义的事件类型”下拉列表中,取消选中除“资源写入成功”之外的所有选项。In the Defined Event Types drop-down, uncheck all options except Resource Write Success.
    • 对于“终结点类型”,选择 WebhookFor Endpoint Type, select Webhook.
    • 单击“选择终结点”。Click Select an endpoint. 在打开的“选择 Web Hook”页上,粘贴你为 Watch-VMWrite Runbook 创建的 Webhook URL。On the Select Web Hook page that opens up, paste the webhook url you created for the Watch-VMWrite runbook.
    • 在“筛选器”下,输入需要在其中查找新创建的 VM 的订阅和资源组。Under FILTERS, enter the subscription and resource group where you want to look for the new VMs created. 它看起来应该如下所示:/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/Microsoft.Compute/virtualMachinesIt should look like: /subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/Microsoft.Compute/virtualMachines
  4. 选择“创建”保存事件网格订阅。Select Create to save the Event Grid subscription.

创建触发 runbook 的 VMCreate a VM that triggers the runbook

  1. 在事件网格订阅前缀筛选器中指定的资源组中创建新的 VM。Create a new VM in the resource group you specified in the Event Grid subscription prefix filter.

  2. 应调用 Watch-VMWrite runbook,且一个新的标记应添加到 VM。The Watch-VMWrite runbook should be called and a new tag added to the VM.

    VM 标记

  3. 向 Microsoft Teams 通道发送了一条新消息。A new message is sent to the Microsoft Teams channel.

    Microsoft Teams 通知

后续步骤Next steps

在本教程中,在事件网格和自动化之间设置集成。In this tutorial, you set up integration between Event Grid and Automation. 你已了解如何:You learned how to:

  • 导入事件网格示例 runbook。Import an Event Grid sample runbook.
  • 创建可选 Microsoft Teams Webhook。Create an optional Microsoft Teams webhook.
  • 为 runbook 创建 Webhook。Create a webhook for the runbook.
  • 创建事件网格订阅。Create an Event Grid subscription.
  • 创建触发 runbook 的 VM。Create a VM that triggers the runbook.