Subscribe to events for a resource group and filter for a resource with PowerShell
This script creates an Event Grid subscription to the events for a resource group. It uses a filter to get only events for a specified resource in the resource group.
If you don't have an Azure subscription, create an Azure free account before you begin.
Sample script - stable
Note
We recommend that you use the Azure Az PowerShell module to interact with Azure. To get started, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to 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
Sample script - preview module
Important
Using this Azure feature from PowerShell requires the AzureRM
module installed. This
is an older module only available for Windows PowerShell 5.1 that no longer receives new features.
The Az
and AzureRM
modules are not compatible when installed for the same versions of PowerShell.
If you need both versions:
- Uninstall the Az module from a PowerShell 5.1 session.
- Install the AzureRM module from a PowerShell 5.1 session.
- Download and install PowerShell Core 6.x or later.
- Install the Az module in a PowerShell Core session.
The preview sample script requires the Event Grid module. To install, run
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)
Script explanation
This script uses the following command to create the event subscription. Each command in the table links to command-specific documentation.
Command | Notes |
---|---|
New-AzEventGridSubscription | Create an Event Grid subscription. |
Next steps
- For an introduction to managed applications, see Azure Managed Application overview.
- For more information on PowerShell, see Azure PowerShell documentation.