PowerShell を使用した Azure サブスクリプションのイベントのサブスクライブ

このスクリプトは、Azure サブスクリプションのイベントに対する Event Grid サブスクリプションを作成します。

Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

サンプル スクリプト - 安定版

注意

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、Azure PowerShell のインストールに関する記事を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

# 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

サンプル スクリプト - プレビュー モジュール

このプレビュー版サンプル スクリプトには、Event Grid モジュールが必要です。 インストールするには、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 Event Grid のサブスクリプションを作成する。

次のステップ