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 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 and subscribe to.
$myResourceGroup="<resource-group-name>"

# Create resource grroup
New-AzResourceGroup -Name $myResourceGroup -Location westus2

# Subscribe to the resource group. Provide the name of the resource group you want to subscribe to.
New-AzEventGridSubscription `
  -Endpoint $myEndpoint `
  -EventSubscriptionName demoSubscriptionToResourceGroup `
  -ResourceGroupName $myResourceGroup

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

重要

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

# Provide the name of the resource group to create and subscribe to.
$myResourceGroup = "<resource-group-name>"

# Create resource group
$resourceGroupID = (New-AzResourceGroup -Name $myResourceGroup -Location westus2).ResourceId

# Subscribe to the resource group. Provide the name of the resource group you want to subscribe to.
New-AzEventGridSubscription `
  -ResourceId $resourceGroupID `
  -Endpoint $myEndpoint `
  -EventSubscriptionName demoSubscriptionToResourceGroup

スクリプトの説明

このスクリプトは、次のコマンドを使用してイベント サブスクリプションを作成します。 表内の各コマンドは、それぞれのドキュメントにリンクされています。

コマンド メモ
New-AzEventGridSubscription Event Grid のサブスクリプションを作成する。

次のステップ