azurepowershellazure-powershellazure-alerts

Create Alert rule using New-AzScheduledQueryRuleConditionObject returns bad term


I'm trying to create a new Alert rule dynamically using DevOps pipelines and Powershell, but it's been really frustrating since documentation states some parameters are available to be passed in, but when running I get the following error:

##[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.

Clearly, Microsoft documentation states it's possible to pass -Operator as a param

$QueryRuleQuery = "let MinTime = ago(5m);\nAzureMetrics\n| where TimeGenerated < MinTime\n    and MetricName == \`"storage_percent\`"\n    and Maximum >= $($Threshold)\n    and (ResourceId has \`"ELASTICPOOLS\`" or ResourceId has \`"DATABASES\`")\n| project ResourceGroup, Resource, MetricName, Maximum,ResourceId\n| distinct ResourceGroup, Resource, MetricName, Maximum,ResourceId\n"
$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

Any guidance is appreciated


Solution

  • Turns out error was on my side. I was accidentally passing a comment on some parameters and that was screwing up the execution.