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

使用New-AzScheduledQueryRuleConditionObject创建警报规则遇参数错误

解决New-AzScheduledQueryRuleConditionObject的-Operator参数识别错误问题

执行PowerShell创建Azure计划查询警报规则时,触发以下错误:

##[error]The term '-Operator' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

微软官方文档明确New-AzScheduledQueryRuleConditionObject支持-Operator参数,问题代码示例如下:

$QueryRuleQuery = "let MinTime = ago(5m);
AzureMetrics
| where TimeGenerated < MinTime
    and MetricName == `"storage_percent`"
    and Maximum >= $($Threshold)
    and (ResourceId has `"ELASTICPOOLS`" or ResourceId has `"DATABASES`")
| project ResourceGroup, Resource, MetricName, Maximum,ResourceId
| distinct ResourceGroup, Resource, MetricName, Maximum,ResourceId
"
$QueryRuleScope = "/subscriptions/$($SubscriptionId)/resourcegroups/$($REDACTED.ToLower())-$($Environment.ToLower())-redacted/providers/microsoft.operationalinsights/workspaces/$($REDACTED.ToLower())-$($RegionCode.ToLower())-$($EnvironmentShort.ToLower())-redacted"
$QueryRuleCondition = New-AzScheduledQueryRuleConditionObject `
    -Query $QueryRuleQuery `
    -TimeAggregation 'Count' `
    -ResourceIdColumn 'Resource' `
    -Operator 'GreaterThanOrEqual' `
    -Threshold 10 `
    -FailingPeriodNumberOfEvaluationPeriod 1 `
    -FailingPeriodMinFailingPeriodsToAlert 1

New-AzScheduledQueryRule `
    -Name $QueryRuleName `
    -ResourceGroupName $ResourceGroupName `
    -Location $RegionCodeDatacenter `
    -ActionGroupResourceId "/subscriptions/$($SubscriptionId)/resourceGroups/$($REDACTED.ToLower())-$($Environment.ToLower())-redacted/providers/microsoft.insights/actionGroups/$($REDACTED.ToLower())-$($RegionCode.ToLower())-$($EnvironmentShort.ToLower())-redacted"
    -Description "This will trigger when an Elastic Pool or Database reaches $($Threshold) percent of maximum size"
    -DisplayName $QueryRuleName `
    -Scope $QueryRuleScope `
    -Severity $QueryRuleSeverityInt `
    -WindowSize 'P1D' `
    -EvaluationFrequency 'P1D' `
    -CriterionAllOf $QueryRuleCondition

解决步骤:

  • 升级Az.Monitor模块:旧版本Az.Monitor可能未引入-Operator参数,运行以下命令更新模块:

    Update-Module -Name Az.Monitor -Force
    

    可通过Get-Module -ListAvailable Az.Monitor查看当前版本。

  • 修复代码换行语法错误:New-AzScheduledQueryRule中-ActionGroupResourceId行末尾缺失反引号(`),导致后续参数被解析为独立命令,引发连锁错误。修正后该行应为:

    -ActionGroupResourceId "/subscriptions/$($SubscriptionId)/resourceGroups/$($REDACTED.ToLower())-$($Environment.ToLower())-redacted/providers/microsoft.insights/actionGroups/$($REDACTED.ToLower())-$($RegionCode.ToLower())-$($EnvironmentShort.ToLower())-redacted" `
    
  • 确认PowerShell环境兼容性:在DevOps流水线中,使用PowerShell Core 7.x版本执行脚本,避免旧版PowerShell的兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 10:55:53