Subskrybowanie zdarzeń dla subskrypcji platformy Azure przy użyciu programu PowerShell

Ten skrypt tworzy subskrypcję usługi Event Grid na potrzeby zdarzeń związanych z subskrypcją platformy Azure.

Jeśli nie masz subskrypcji platformy Azure, przed rozpoczęciem utwórz bezpłatne konto platformy Azure.

Przykładowy skrypt — stabilny

Uwaga

Do interakcji z platformą Azure zalecamy używanie modułu Azure Az w programie PowerShell. Zobacz Instalowanie programu Azure PowerShell, aby rozpocząć. Aby dowiedzieć się, jak przeprowadzić migrację do modułu Az PowerShell, zobacz Migracja programu Azure PowerShell z modułu AzureRM do modułu 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

Przykładowy skrypt — moduł w wersji zapoznawczej

Ten przykładowy skrypt w wersji zapoznawczej wymaga modułu Event Grid. Aby go zainstalować, uruchom polecenie Install-Module -Name AzureRM.EventGrid -AllowPrerelease -Force -Repository PSGallery

Ważne

Korzystanie z tej funkcji platformy Azure z poziomu programu PowerShell wymaga zainstalowania modułu AzureRM . Jest to starszy moduł dostępny tylko dla programu Windows PowerShell 5.1, który nie otrzymuje już nowych funkcji. Moduły Az i AzureRM niezgodne w przypadku instalacji dla tych samych wersji programu PowerShell. Jeśli potrzebujesz obu wersji:

  1. Odinstaluj moduł Az z sesji programu PowerShell 5.1.
  2. Zainstaluj moduł AzureRM z sesji programu PowerShell 5.1.
  3. Pobierz i zainstaluj program PowerShell Core 6.x lub nowszy.
  4. Zainstaluj moduł Az w sesji programu 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

Objaśnienia dla skryptu

Ten skrypt używa następującego polecenia w celu utworzenia subskrypcji zdarzeń. Każde polecenie w tabeli stanowi link do dokumentacji polecenia.

Polecenie Uwagi
New-AzEventGridSubscription Tworzy subskrypcję usługi Event Grid.

Następne kroki