Muokkaa

Jaa


Subscribe to events for a resource group with PowerShell

This script creates an Event Grid subscription to the events for a resource group.

If you don't have an Azure subscription, create an Azure free account before you begin.

The preview sample script requires the Event Grid module. To install, run Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery

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 and subscribe to.
$myResourceGroup="<resource-group-name>"

# Create resource grroup
New-AzResourceGroup -Name $myResourceGroup -Location westus2

# 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

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:

  1. Uninstall the Az module from a PowerShell 5.1 session.
  2. Install the AzureRM module from a PowerShell 5.1 session.
  3. Download and install PowerShell Core 6.x or later.
  4. Install the Az module in a 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 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 and subscribe to.
$myResourceGroup = "<resource-group-name>"

# Create resource group
$resourceGroupID = (New-AzResourceGroup -Name $myResourceGroup -Location westus2).ResourceId

# Subscribe to the resource group. Provide the name of the resource group you want to subscribe to.
New-AzEventGridSubscription `
  -ResourceId $resourceGroupID `
  -Endpoint $myEndpoint `
  -EventSubscriptionName demoSubscriptionToResourceGroup

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