你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 PowerShell 订阅资源组的事件

此脚本创建资源组事件的事件网格订阅。

如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

预览示例脚本需要事件网格模块。 若要安装,请运行 Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery

示例脚本 - 稳定版

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

# 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 westus2

# 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

示例脚本 - 预览模块

重要

使用 PowerShell 的 Azure 功能必须已安装 AzureRM 模块。 该模块是旧模块,仅适用于 Windows PowerShell 5.1,它不再接收新功能。 针对相同版本的 PowerShell 进行安装时,AzAzureRM 模块不兼容。 如果需要两个版本,请执行以下操作:

  1. 在 PowerShell 5.1 会话中卸载 Az 模块
  2. 在 PowerShell 5.1 会话中安装 AzureRM 模块
  3. 下载并安装 PowerShell Core 6.x 或更高版本
  4. 在 PowerShell Core 会话中安装 Az 模块
# 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 westus2).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

脚本说明

此脚本使用以下命令创建事件订阅。 表中的每条命令链接到特定于命令的文档。

命令 说明
New-AzEventGridSubscription 创建事件网格订阅。

后续步骤