You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure PowerShell 12.1.0中Event Grid域订阅高级过滤器值无法设置求助

Azure PowerShell 12.1.0下Event Grid域订阅高级过滤器Values/Value无法设置的解决方案

在Azure DevOps经典发布管道中运行的Azure PowerShell代码,原本用于为Azure Event Grid域订阅配置高级过滤器,在微软将代理升级到azure pipelines/windows-latest(对应Azure PowerShell 12.1.0版本)后失效。重构后大部分功能恢复,但高级过滤器的Values/Value属性无法被正确设置,仅Key和OperatorType能正常生效。

原代码示例:

# Define the advanced filters
$advancedFilters = @(
    @{
        OperatorType = "StringIn";
        Key = "subject";
        Values = @("example1", "example2")
    },
    @{
        OperatorType = "NumberGreaterThan";
        Key = "data.size";
        Value = 100
    }
)

# Create the Event Grid domain subscription with advanced filters
New-AzEventGridDomainEventSubscription `
    -ResourceGroupName "yourResourceGroupName" `
    -DomainName "yourDomainName" `
    -EventSubscriptionName "yourSubscriptionName" `
    -FilterIncludedEventType @("Microsoft.Storage.BlobCreated", 
                               "Microsoft.Storage.BlobDeleted") `
    -Destination @{
        EndpointType = "WebHook";
        Properties = @{
            EndpointUrl = "https://your-webhook-endpoint"
        }
    } `
    -FilterEnableAdvancedFilteringOnArray $true `
    -FilterAdvancedFilter $advancedFilters

查阅New-AzEventGridDomainEventSubscription文档可知,-FilterAdvancedFilter参数接受IAdvancedFilter[]集合,但IAdvancedFilter接口仅定义了Key和OperatorType属性,并未包含Values或Value。此前的示例代码均采用哈希表方式配置,但升级后该方式不再生效。Az CLI的等效代码可正常运行,但切换成本过高;尝试Update-AzEventGridDomainEventSubscription命令也无法解决问题。


解决方案

问题根源在于Az PowerShell 12.x版本对参数类型的检查变得更严格,之前通过哈希表隐式转换为IAdvancedFilter的方式,无法自动将Values/Value映射到具体过滤器子类的属性上。需要显式创建对应OperatorType的过滤器子类对象,而非使用通用哈希表。

修改后的代码示例:

# 创建对应类型的高级过滤器对象
# StringIn类型过滤器,使用Values属性
$stringInFilter = New-Object -TypeName Microsoft.Azure.Management.EventGrid.Models.StringInFilter
$stringInFilter.Key = "subject"
$stringInFilter.Values = @("example1", "example2")

# NumberGreaterThan类型过滤器,使用Value属性
$numberGreaterFilter = New-Object -TypeName Microsoft.Azure.Management.EventGrid.Models.NumberGreaterThanFilter
$numberGreaterFilter.Key = "data.size"
$numberGreaterFilter.Value = 100

# 组装过滤器数组
$advancedFilters = @($stringInFilter, $numberGreaterFilter)

# 创建Event Grid域订阅
New-AzEventGridDomainEventSubscription `
    -ResourceGroupName "yourResourceGroupName" `
    -DomainName "yourDomainName" `
    -EventSubscriptionName "yourSubscriptionName" `
    -FilterIncludedEventType @("Microsoft.Storage.BlobCreated", 
                               "Microsoft.Storage.BlobDeleted") `
    -Destination @{
        EndpointType = "WebHook";
        Properties = @{
            EndpointUrl = "https://your-webhook-endpoint"
        }
    } `
    -FilterEnableAdvancedFilteringOnArray $true `
    -FilterAdvancedFilter $advancedFilters

关键说明:

  • 每种OperatorType对应一个具体的过滤器类(如StringInFilter、NumberGreaterThanFilter、BoolEqualsFilter等),这些子类继承自IAdvancedFilter,并定义了对应的Value或Values属性。
  • 显式创建这些子类对象后,属性会被正确识别并传递给Azure API,从而完成高级过滤器值的配置。

内容的提问来源于stack exchange,提问作者grahama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 16:40:16