Share via


Sottoscrivere eventi per un gruppo di risorse e applicare un filtro per una risorsa con PowerShell

Lo script crea una sottoscrizione di Griglia di eventi per gli eventi relativi a un gruppo di risorse. Usa un filtro per ottenere solo gli eventi per una risorsa specifica nel gruppo di risorse.

Se non si ha una sottoscrizione di Azure, creare un account Azure gratuito prima di iniziare.

Script di esempio - stabile

Nota

È consigliabile usare il modulo Azure Az PowerShell per interagire con Azure. Per iniziare, vedere Installare Azure PowerShell. Per informazioni su come eseguire la migrazione al modulo AZ PowerShell, vedere Eseguire la migrazione di Azure PowerShell da AzureRM ad 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 di esempio: modulo di anteprima

Importante

Se si usa questa funzionalità di Azure da PowerShell, è necessario che sia installato il modulo AzureRM. Si tratta di un modulo meno recente, disponibile solo per Windows PowerShell 5.1.x, che non riceve più le nuove funzionalità. I moduli Az e AzureRMnon sono compatibili con quando vengono installati per le stesse versioni di PowerShell. Se sono necessarie entrambe le versioni:

  1. Disinstallare il modulo Az da una sessione di PowerShell 5.1.
  2. Installare il modulo AzureRM da una sessione di PowerShell 5.1.
  3. Scaricare e installare PowerShell Core 6.x o versioni successive.
  4. Installare il modulo Az in una sessione di PowerShell Core.

Lo script di esempio di anteprima richiede il modulo Griglia di eventi. Per installarlo, eseguire 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)

Spiegazione dello script

Questo script usa il comando seguente per creare una sottoscrizione a eventi. Ogni comando della tabella include collegamenti alla documentazione specifica del comando.

Comando Note
New-AzEventGridSubscription Creare una sottoscrizione di Griglia di eventi.

Passaggi successivi