Suscripción a eventos de un tema personalizado con PowerShell

Este script crea una suscripción de Event Grid a los eventos de un tema personalizado.

Si no tiene una suscripción a Azure, cree una cuenta gratuita de Azure antes de empezar.

El script de ejemplo de la versión preliminar requiere el módulo de Event Grid. Para instalarlo, ejecute Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery.

Script de ejemplo: estable

Nota:

Se recomienda usar el módulo Azure Az de PowerShell para interactuar con Azure. Consulte Instalación de Azure PowerShell para empezar. Para más información sobre cómo migrar al módulo Az de PowerShell, consulte Migración de Azure PowerShell de AzureRM a 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

Script de ejemplo: módulo de la versión preliminar

Importante

El uso de esta característica de Azure desde PowerShell requiere que tenga el módulo AzureRM instalado. Se trata de un módulo anterior que solo está disponible para Windows PowerShell 5.1 que ya no obtiene nuevas características. Los módulos Az y AzureRMno son compatibles cuando se instalan para las mismas versiones de PowerShell. Si necesita ambas versiones:

  1. Desinstale el módulo Az desde una sesión de PowerShell 5.1.
  2. Instale el módulo AzureRM desde una sesión de PowerShell 5.1.
  3. Descargue e instale PowerShell Core 6.x o posterior.
  4. Instale el módulo Az en una sesión de 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 

Explicación del script

Este script usa el siguiente comando para crear la suscripción de eventos. Cada comando de la tabla crea un vínculo a documentación específica del comando.

Get-Help Notas
New-AzEventGridSubscription Cree una suscripción de Event Grid.

Pasos siguientes