使用 PowerShell 订阅资源组的事件Subscribe to events for a resource group with PowerShell
此脚本创建资源组事件的事件网格订阅。This script creates an Event Grid subscription to the events for a resource group.
如果没有 Azure 订阅,可在开始前创建一个试用帐户。If you don't have an Azure subscription, create a trial account before you begin.
预览示例脚本需要事件网格模块。The preview sample script requires the Event Grid module. 若要安装,请运行 Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery
To install, run Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery
示例脚本 - 稳定版Sample script - stable
备注
本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介。To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShell。For Az module installation instructions, see Install Azure PowerShell.
# Provide an endpoint for handling the events. Must be formatted "https://your-endpoint-URL"
$myEndpoint = "<your-endpoint-URL>"
# Provide the name of the resource group to create and subscribe to.
$myResourceGroup="<resource-group-name>"
# Create resource grroup
New-AzResourceGroup -Name $myResourceGroup -Location chinanorth2
# Subscribe to the resource group. Provide the name of the resource group you want to subscribe to.
New-AzEventGridSubscription `
-Endpoint $myEndpoint `
-EventSubscriptionName demoSubscriptionToResourceGroup `
-ResourceGroupName $myResourceGroup
示例脚本 - 预览模块Sample script - preview module
重要
使用 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 进行安装时,Az
和 AzureRM
模块不兼容。The Az
and AzureRM
modules are not compatible when installed for the same versions of PowerShell.
如果需要两个版本,请执行以下操作:If you need both versions:
- 在 PowerShell 5.1 会话中卸载 Az 模块。Uninstall the Az module from a PowerShell 5.1 session.
- 在 PowerShell 5.1 会话中安装 AzureRM 模块。Install the AzureRM module from a PowerShell 5.1 session.
- 下载并安装 PowerShell Core 6.x 或更高版本。Download and install PowerShell Core 6.x or later.
- 在 PowerShell Core 会话中安装 Az 模块。Install the Az module in a PowerShell Core session.
# You must have the latest version of the Event Grid PowerShell module.
# To install:
# Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery
# Provide an endpoint for handling the events. Must be formatted "https://your-endpoint-URL"
$myEndpoint = "<your-endpoint-URL>"
# Provide the name of the resource group to create and subscribe to.
$myResourceGroup = "<resource-group-name>"
# Create resource group
$resourceGroupID = (New-AzResourceGroup -Name $myResourceGroup -Location chinanorth2).ResourceId
# Subscribe to the resource group. Provide the name of the resource group you want to subscribe to.
New-AzEventGridSubscription `
-ResourceId $resourceGroupID `
-Endpoint $myEndpoint `
-EventSubscriptionName demoSubscriptionToResourceGroup
脚本说明Script explanation
此脚本使用以下命令创建事件订阅。This script uses the following command to create the event subscription. 表中的每条命令链接到特定于命令的文档。Each command in the table links to command-specific documentation.
CommandCommand | 说明Notes |
---|---|
New-AzEventGridSubscriptionNew-AzEventGridSubscription | 创建事件网格订阅。Create an Event Grid subscription. |
后续步骤Next steps
- 有关托管应用程序的简介,请参阅 Azure 托管应用程序概述。For an introduction to managed applications, see Azure Managed Application overview.
- 有关 PowerShell 的详细信息,请参阅 Azure PowerShell 文档。For more information on PowerShell, see Azure PowerShell documentation.