Share via


Inscreva-se em eventos para um tópico personalizado com o PowerShell

Este script cria uma assinatura de Grade de Eventos para os eventos para um tópico personalizado.

Caso você não tenha uma assinatura do Azure, crie uma conta gratuita do Azure antes de começar.

O script de exemplo de visualização requer o módulo de Grade de Eventos. Para instalar, execute Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery

Exemplo de script - estável

Observação

Recomendamos que você use o módulo Az PowerShell do Azure para interagir com o Azure. Confira Instalar o Azure PowerShell para começar. Para saber como migrar para o módulo Az PowerShell, confira Migrar o Azure PowerShell do AzureRM para o 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

Exemplo de script – módulo de visualização

Importante

Usar esse recurso do Azure do PowerShell exige que você tenha o módulo AzureRM instalado. Esse é um módulo mais antigo disponível apenas para o Windows PowerShell 5.1. que não recebe mais novos recursos. Os módulos Az e AzureRMnão são compatíveis quando instalados para as mesmas versões do PowerShell. Se você precisar das duas versões:

  1. Desinstale o módulo Az de uma sessão do PowerShell 5.1.
  2. Instale o módulo AzureRM de uma sessão do PowerShell 5.1.
  3. Baixe e instale o PowerShell Core 6.x ou posterior.
  4. Instale o módulo Az em uma sessão do 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 

Explicação sobre o script

Este script usa o seguinte comando para criar a assinatura do evento. Cada comando na tabela redireciona para a documentação específica do comando.

Comando Observações
New-AzEventGridSubscription Criar uma assinatura na Grade de Eventos.

Próximas etapas