您现在访问的是微软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 the AzureRM module installed. 该模块是旧模块,仅适用于 Windows PowerShell 5.1,它不再接收新功能。This is an older module only available for Windows PowerShell 5.1 that no longer receives new features. 针对相同版本的 PowerShell 进行安装时,AzAzureRM 模块不兼容。 The Az and AzureRM modules are not compatible when installed for the same versions of PowerShell. 如果需要两个版本,请执行以下操作:If you need both versions:

  1. 在 PowerShell 5.1 会话中卸载 Az 模块Uninstall the Az module from a PowerShell 5.1 session.
  2. 在 PowerShell 5.1 会话中安装 AzureRM 模块Install the AzureRM module from a PowerShell 5.1 session.
  3. 下载并安装 PowerShell Core 6.x 或更高版本Download and install PowerShell Core 6.x or later.
  4. 在 PowerShell Core 会话中安装 Az 模块Install the Az module in a PowerShell Core session.

若要完成此教程,需要 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.

  6. 更新脚本中的第 74 行以使用 Tag 而不是 TagsUpdate the line 74 in the script to use Tag instead of Tags.

    Update-AzureRmVM -ResourceGroupName $VMResourceGroup -VM $VM -Tag $Tag | Write-Verbose
    
  7. 选择“发布”按钮 。Select the Publish button.

创建可选 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 URL 复制到剪贴板并保存。Copy the webhook URL 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:

    1. 对于“主题类型”,选择“Azure 订阅” 。For Topic Type, select Azure Subscriptions.

    2. 取消选中“订阅所有事件类型”复选框 。Uncheck the Subscribe to all event types check box.

    3. 输入 AzureAutomation 作为名称 。Enter AzureAutomation for the name.

    4. 在“定义的事件类型” 下拉列表中,取消选中除“资源写入成功” 之外的所有选项。In the Defined Event Types drop-down, uncheck all options except Resource Write Success.

      备注

      Azure 资源管理器当前不区分创建和更新,因此在 Azure 订阅中对所有 Microsoft.Resources.ResourceWriteSuccess 事件实现本教程可能会导致大量调用。Azure Resource Manager does not currently differentiate between Create and Update, so implementing this tutorial for all Microsoft.Resources.ResourceWriteSuccess events in your Azure Subscription could result in a high volume of calls.

    5. 对于“终结点类型” ,选择 WebhookFor Endpoint Type, select Webhook.

    6. 单击“选择终结点” 。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.

    7. 在“筛选器”下,输入需要在其中查找新创建的 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.