Unable to create MetricAlert using Add-AzMetricAlertRuleV2

52 views Asked by At

I am trying to create a MetricAlert using the following script

Set-AzContext -Subscription "f0bXXXXXXX"

$action = Get-AzActionGroup -ResourceGroupName gze-actgrp-pd-rgp-001 -Name UWDevOps_PD

$actionID = New-AzActionGroup -ActionGroupId $action.id

#Get-AzMetricDefinition -ResourceId "/subscriptions/9ca95ff2-f1fd-447d-9130-9081d2ba12be/resourcegroups/gze-pdi203-pd1-rgp-001/providers/Microsoft.ContainerService/managedClusters/gze-pdi203-pd1-aks-isostr-001"

#set alert dim to pick the right options
$dim = New-AzMetricAlertRuleV2DimensionSelection -DimensionName "Name of the nodepool" -ValuesToInclude "pdi203pd101"

$severity = 1

$RGObject = "gze-pdi203-pd1-rgp-001"

$ResourceId = "/subscriptions/9caXXXXXXX/resourcegroups/gze-pdi203-pd1-rgp-001/providers/Microsoft.ContainerService/managedClusters/gze-pdi203-pd1-aks-isostr-001"

#set alert criteria and counter % Processor Time
$criteria = New-AzMetricAlertRuleV2Criteria -MetricName "node_disk_usage_percentage" -DimensionSelection $dim
-TimeAggregation average -Operator GreaterThan
-Threshold 90

Add-AzMetricAlertRuleV2 -Name "UWS-AKS203-ISOSTR-PD1-[Sev1-Error]-Cluster-DiskUtilization_Morethan90Percnt-V2-CT" -ResourceGroupName $RGObject
-WindowSize 01:00:00 -Frequency 01:00:00
-TargetResourceId $ResourceId -Condition $criteria
-ActionGroup $action.Id `
-Severity $severity

But then it fails here

$actionID = New-AzActionGroup -ActionGroupId $action.id

saying that

New-AzActionGroup is not recognized.

I have Az.Monitor 4.5 ver, AZ module ver 10.0.0 and windows PS ver 5.1 On reading some more blogs, it was made clear that New-AzActionGroup was not available in Az.MOnitor 4.5 and was only until ver 3.1.0. I tried to force install 3.1.0 using

Install-Module -Name Az.Monitor -RequiredVersion 3.0.1 -Force

but that did not install 3.0.1 either.

1

There are 1 answers

0
Jahnavi On

Firstly, check the current version of the Az.Monitor installed module using below command.

Get-InstalledModule -NAme "Az.Monitor"

enter image description here

In any case, if you want to run the script with a different version than the one that is now installed, you must first unload the module with the Remove-Module command and then remove it with the uninstall-module command.

You can now use the other version of the module by reinstalling it using Install-Module -Name xxxx -Requiredversion xxxx.

Regarding your issue, after a workaround on it I found an alternative by installing Az.Resources module. The Az.Resources module, which is an accessible module for all Azure resources, contains the New-AzActionGroup command.

Use below commands in PowerShell to install and import the Az.Resources module.

Install-Module -Name Az.Resources -Force
Import-Module -Name Az.Resources 

Once it is done, I tried to execute the below commands related to the creation of action group and was successful as shown.

$action = Get-Azactiongroup -ResourceGroupName xxxx -Name actionadd
New-AzActionGroup -ActionGroupId $action.Id 

enter image description here

enter image description here

enter image description here

Alternatively, you can also use az monitor action-group create CLI command in Azure bash to achieve your requirement without any conflicts.

az monitor action-group create --action webhook MyAction https://alerts.xxxx.comxxx type=HighCPU --name jahact --resource-group xxxx

enter image description here