使用 PowerShell 订阅 Azure 订阅的事件

此脚本创建 Azure 订阅事件的事件网格订阅。

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

示例脚本 - 稳定版

注意

建议使用 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>"

# Select the Azure subscription you want to subscribe to. You need this command only if the 
# current subscription is not the one you wish to subscribe to.
Set-AzContext -Subscription "<subscription-name-or-ID>"

# Subscribe to the Azure subscription. The command creates the subscription for the currently selected Azure subscription. 
New-AzEventGridSubscription -Endpoint $myEndpoint -EventSubscriptionName demoSubscriptionToAzureSub

示例脚本 - 预览模块

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

重要

使用 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>"

# Get the subscription ID
$subID = (Get-AzureRmSubscription -SubscriptionName "<subscription-name>").Id

# Subscribe to the Azure subscription. The command creates the subscription for the currently selected Azure subscription. 
New-AzureRmEventGridSubscription -ResourceId "/subscriptions/$subID" -Endpoint $myEndpoint -EventSubscriptionName demoSubscriptionToAzureSub

脚本说明

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

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

后续步骤