Inscrever-se em eventos de uma assinatura do Azure com o PowerShell

Este script cria uma assinatura de Grade de Eventos para os eventos de uma assinatura do Azure.

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

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 an endpoint for handling the events. Must be formatted "https://your-endpoint-URL"
$myEndpoint = "<your-endpoint-URL>"

# Select the Azure subscription you want to subscribe to. You need this command only if the 
# current subscription is not the one you wish to subscribe to.
Set-AzContext -Subscription "<subscription-name-or-ID>"

# Subscribe to the Azure subscription. The command creates the subscription for the currently selected Azure subscription. 
New-AzEventGridSubscription -Endpoint $myEndpoint -EventSubscriptionName demoSubscriptionToAzureSub

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

Este script de exemplo de versão prévia exige o módulo da Grade de Eventos. Para instalar, execute Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery

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 an endpoint for handling the events. Must be formatted "https://your-endpoint-URL"
$myEndpoint = "<your-endpoint-URL>"

# Get the subscription ID
$subID = (Get-AzureRmSubscription -SubscriptionName "<subscription-name>").Id

# Subscribe to the Azure subscription. The command creates the subscription for the currently selected Azure subscription. 
New-AzureRmEventGridSubscription -ResourceId "/subscriptions/$subID" -Endpoint $myEndpoint -EventSubscriptionName demoSubscriptionToAzureSub

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