使用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
相关产品推荐
相关产品推荐

