Berlangganan ke peristiwa untuk topik kustom dengan PowerShell

Skrip ini membuat langganan Event Grid ke acara untuk topik kustom.

Jika Anda tidak memiliki Langganan Azure, buat Akun gratis Azure sebelum memulai.

Sampel pratinjau skrip memerlukan modul Event Grid. Untuk menginstal, jalankan Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery

Skrip sampel - stabil

Catatan

Sebaiknya Anda menggunakan modul Azure Az PowerShell untuk berinteraksi dengan Azure. Lihat Menginstal Azure PowerShell untuk memulai. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

# 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

Sampel skrip - modul pratinjau

Penting

Untuk menggunakan fitur Azure ini dari PowerShell wajib menginstal modul AzureRM. Ini adalah modul lama yang hanya tersedia untuk Windows PowerShell 5.1 yang tidak lagi mendapatkan fitur baru. Modul Az dan AzureRMtidak kompatibel ketika diinstal untuk versi PowerShell yang sama. Jika Anda memerlukan kedua versi:

  1. Hapus instalan modul Az dari sesi PowerShell 5.1.
  2. Instal modul AzureRM dari sesi PowerShell 5.1.
  3. Unduh dan instal PowerShell Core 6.x atau yang lebih baru.
  4. Instal modul Az dalam sesi PowerShell Core.
# 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 

Penjelasan skrip

Skrip ini menggunakan perintah berikut untuk membuat langganan peristiwa. Setiap perintah dalam tabel ditautkan ke dokumentasi spesifik-perintah.

Perintah Catatan
New-AzEventGridSubscription Membuat langganan Event Grid.

Langkah berikutnya