Suscripción a eventos de un grupo de recursos y filtrado de un recurso con PowerShell

Este script crea una suscripción de Event Grid a los eventos de un grupo de recursos. Utiliza un filtro para obtener solo los eventos de un recurso especificado en el grupo de recursos.

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

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 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 network security group.
# You will subscribe to events for this resource group.
$myResourceGroup = "<resource-group-name>"

# Provide a name for the network security group to create.
$nsgName = "<your-nsg-name>"

# Create the resource group
New-AzResourceGroup -Name $myResourceGroup -Location westus2

# Create a network security group. You will filter events to only those that are related to this resource.
New-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $myResourceGroup  -Location westus2

# Get the resource ID to filter events. The name of the network security group must not be the same as the other resource names.
$resourceId = (Get-AzResource -ResourceName $nsgName -ResourceGroupName $myResourceGroup).ResourceId

# 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 `
  -SubjectBeginsWith $resourceId

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.

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.

# 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 custom topic to create
$topicName = "<your-topic-name>"

# Provide the name of the resource group to create. It will contain the custom topic.
$myResourceGroup= "<resource-group-name>"

# Create the resource group
New-AzResourceGroup -Name $myResourceGroup -Location westus2

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

# Get resource ID of custom topic
$topicid = (Get-AzEventGridTopic -ResourceGroupName $myResourceGroup -Name $topicName).Id

# Set the operator type, field and values for the filtering
$AdvFilter1=@{operator="StringIn"; key="Data.color"; Values=@('blue', 'red', 'green')}

# Subscribe to the custom topic. Filter based on a value in the event data.
New-AzEventGridSubscription `
  -ResourceId $topicid `
  -EventSubscriptionName demoSubWithFilter `
  -Endpoint $myEndpoint `
  -AdvancedFilter @($AdvFilter1)

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