Prenumerera på händelser för ett anpassat ämne med PowerShell

Det här skriptet skapar en Event Grid-prenumeration på händelser för ett anpassat ämne.

Om du inte har en Azure-prenumeration skapar du ett kostnadsfritt Azure-konto innan du börjar.

Förhandsversionen av exempelskriptet kräver Event Grid-modulen. Installera genom att köra Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery

Exempelskript – stabilt

Kommentar

Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Se Installera Azure PowerShell för att komma igång. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till 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

Exempelskript – modul för förhandsversion

Viktigt!

Om du använder den här Azure-funktionen från PowerShell måste modulen AzureRM vara installerad. Det här är en äldre modul som bara är tillgänglig för Windows PowerShell 5.1 som inte längre tar emot nya funktioner. Modulerna Az och AzureRM är inte kompatibla när de installeras för samma versioner av PowerShell. Om du behöver båda versionerna:

  1. Avinstallera Az-modulen från en PowerShell 5.1-session.
  2. Installera AzureRM-modulen från en PowerShell 5.1-session.
  3. Ladda ned och installera PowerShell Core 6.x eller senare.
  4. Installera Az-modulen i en PowerShell Core-session.
# 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 

Förklaring av skript

Det här skriptet använder följande kommandon för att skapa händelseprenumerationen. Varje kommando i tabellen länkar till kommandospecifik dokumentation.

Command OBS!
New-AzEventGridSubscription Skapa en Event Grid-prenumeration.

Nästa steg