Share via


PowerShell を使用したカスタム トピックのイベントのサブスクライブ

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

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

プレビュー版サンプル スクリプトには、Event Grid モジュールが必要です。 インストールするには、Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery を実行します

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

注意

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

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

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

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

# Create custom topic and get its resource ID.
$topicID = (New-AzEventGridTopic -ResourceGroupName $myResourceGroup -Name $myTopic -Location westus2).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 Event Grid のサブスクリプションを作成する。

次のステップ