分享方式:


使用 PowerShell 訂閱資源群組的事件和篩選資源

此指令碼可針對資源群組的事件建立 Event Grid 訂用帳戶。 它會使用篩選條件以便只取得資源群組中指定資源的事件。

如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

範例指令碼 - Stable

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 若要開始使用,請參閱 安裝 Azure PowerShell。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 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

範例指令碼 - 預覽版模組

重要

使用 PowerShell 中的 Azure 功能需要安裝 AzureRM 模組。 這是一個較舊的模組,僅適用於 Windows PowerShell 5.1,而且不會再取得新的功能。 若在相同版本的 PowerShell 上安裝 AzAzureRM 模組,這兩個模組不會相容。 如果您需要這兩個版本:

  1. 從 PowerShell 5.1 工作階段中解除安裝 Az 模組
  2. 從 PowerShell 5.1 工作階段中安裝 AzureRM 模組
  3. 下載並安裝 PowerShell Core 6.x 或更新版本
  4. 在 PowerShell Core 工作階段中安裝 Az 模組

預覽範例指令碼需要事件方格模組。 若要安裝,請執行 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)

指令碼說明

此指令碼會使用下列命令來建立事件訂用帳戶。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
New-AzEventGridSubscription 建立事件格線訂用帳戶。

下一步