使用 PowerShell 订阅自定义主题的事件

此脚本创建自定义主题事件的事件网格订阅。

如果没有 Azure 订阅,可在开始前创建一个试用帐户

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

示例脚本 - 稳定版

注意

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

# Provide the name of the topic you are subscribing to
$myTopic = "<your-custom-topic-name>"

# Provide an endpoint for handling the events. Must be formatted "https://your-endpoint-URL"
$myEndpoint = "<your-endpoint-URL>"

# Provide a name for resource group to create. It will contain the custom event.
$myResourceGroup = "<resource-group-name>"

# Create resource group
New-AzResourceGroup -Name $myResourceGroup -Location chinanorth2

# Create custom topic
New-AzEventGridTopic -ResourceGroupName $myResourceGroup -Name $myTopic -Location chinanorth2 

# Subscribe to the custom event. Include the resource group that contains the custom topic.
New-AzEventGridSubscription `
  -EventSubscriptionName demoSubscription `
  -Endpoint $myEndpoint `
  -ResourceGroupName $myResourceGroup `
  -TopicName $myTopic

示例脚本 - 预览模块

重要

使用 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 the name of the topic you are subscribing to
$myTopic = "<your-custom-topic-name>"

# 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. It will contain the custom topic.
$myResourceGroup = "<resource-group-name>"

# Create resource group
New-AzResourceGroup -Name $myResourceGroup -Location chinanorth2

# Create custom topic and get its resource ID.
$topicID = (New-AzEventGridTopic -ResourceGroupName $myResourceGroup -Name $myTopic -Location chinanorth2).Id 

# Subscribe to the custom event. Include the resource group that contains the custom topic.
New-AzEventGridSubscription `
  -ResourceId $topicID `
  -EventSubscriptionName demoSubscription `
  -Endpoint $myEndpoint 

脚本说明

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

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

后续步骤