0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Azure PowerShellにてUpdate-AzScheduledQueryRuleがBadRequestエラーになる場合の対処

Posted at

発生している事象

AzureのログアラートをPowerShellコマンドから有効化・無効化したい場合

Update-AzScheduledQueryRule -Name "alert name" -ResourceGroupName "rgname" -Enabled $true

といったコマンドを実行する必要がありますが、仕様なのかバグなのかこのまま実装するとBadRequestのエラーになってしまいます。

Update-AzScheduledQueryRule: Exception type: Exception, Message: System.Exception: Error in updating Log Alert Rule
 ---> System.AggregateException: One or more errors occurred. (Operation returned an invalid status code 'BadRequest')
 ---> Microsoft.Azure.Management.Monitor.Models.ErrorContractException: Operation returned an invalid status code 'BadRequest'
   at Microsoft.Azure.Management.Monitor.ScheduledQueryRulesOperations.UpdateWithHttpMessagesAsync(String resourceGroupName, String ruleName, LogSearchRuleResourcePatch parameters, Dictionary`2 customHeaders, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at Microsoft.Azure.Commands.Insights.ScheduledQueryRules.UpdateScheduledQueryRuleCommand.ProcessRecordInternal()
   --- End of inner exception stack trace ---
   at Microsoft.Azure.Commands.Insights.ScheduledQueryRules.UpdateScheduledQueryRuleCommand.ProcessRecordInternal()
   at Microsoft.Azure.Commands.Insights.MonitorCmdletBase.ExecuteCmdlet(), Code: Null, Status code:Null, Reason phrase: Null

解決策

アラートルールのクエリの中身を確認すると、最終アラートクエリとしてこのようになっていると思います。TimeGeneratedとAggregatedValueの文がGUIで設定した内容に基づいて追加されていると思います。
image.png

ここが落とし穴なのですが、このAggregatedValueの部分を明示的にログクエリの中に記載しないとAzurePowerShellのコマンドからはエラーになるようです(二重になるのが気持ち悪いですが)。
image.png

最終的なクエリは以下になります。無駄にAggregatedValueのところが二重になっていて美しくはないですね。
image.png

でもこの設定をしてから実行するとしっかり動きました。

Update-AzScheduledQueryRule -Name testlogalertrule -ResourceGroupName AVDTESTRG -Enabled $true
WARNING: 12:53:28 AM - The namespace for all the model classes will change from Microsoft.Azure.Management.Monitor.Management.Models to Microsoft.Azure.Management.Monitor.Models in future releases.
WARNING: 12:53:28 AM - The namespace for output classes will be uniform for all classes in future releases to make it independent of modifications in the model classes.

CreatedWithApiVersion    :
IsLegacyLogAnalyticsRule :
Description              :
DisplayName              :
AutoMitigate             :
Enabled                  : true
LastUpdatedTime          : 1/27/2022 12:53:37 AM
ProvisioningState        : Succeeded
Source                   : Microsoft.Azure.Management.Monitor.Models.Source
Schedule                 : Microsoft.Azure.Management.Monitor.Models.Schedule
Action                   : Microsoft.Azure.Management.Monitor.Models.AlertingAction
Id                       : /subscriptions/e8d922e3-90d9-4ca9-b817-b9c8f7b08eb3/resourceGroups/AVDTESTRG/providers/microsoft.insights/scheduledqueryrules/tes
                           tlogalertrule
Name                     : testlogalertrule
Type                     : microsoft.insights/scheduledqueryrules
Location                 : japaneast
Tags                     : {}
Kind                     :
Etag                     :



解決策その2

az CLIを使う。この方法で解決した方もいるようです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?