Share via


使用 PowerShell 訂閱 Azure 訂用帳戶的事件

此指令碼可針對 Azure 訂用帳戶的事件建立 Event Grid 訂用帳戶。

如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

範例指令碼 - Stable

注意

建議您使用 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

指令碼說明

此指令碼會使用下列命令來建立事件訂用帳戶。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
New-AzEventGridSubscription 建立事件格線訂用帳戶。

下一步