Sourav Karmakar
Sourav Karmakar

Reputation: 95

Powershell command to fetch Azure monitor alert rules is not working

I have created an alert rule and associated it with a VM. Now trying to fetch the alert rule through Powershell, but getting null. What's wrong with this code ?

Get-AzAlertRule -ResourceGroupName 'pacbldnew'

see the alert rule powershell code returning null

Upvotes: 1

Views: 5233

Answers (4)

Nishchay Chopra
Nishchay Chopra

Reputation: 1

This query has worked for me:

Get-AzResource -ResourceType "microsoft.insights/scheduledqueryrules" -ResourceGroupName "Alert-RG"

Upvotes: 0

Himanshuk
Himanshuk

Reputation: 185

Go to Azure- home security center and settings and filter and extract all rules

Upvotes: 0

Joy Wang
Joy Wang

Reputation: 42163

That is just a warning. The command should work, make sure the alert rule is existing.

enter image description here

Update1:

Try the command below to get what you want.

enter image description here

Get-AzResource -ResourceGroupName joywebapp -ResourceType microsoft.insights/metricAlerts 

enter image description here

Update2:

If you want to get the details, try the script as below.

$names = (Get-AzResource -ResourceGroupName joywebapp -ResourceType microsoft.insights/metricAlerts).Name
foreach($name in $names){
    Get-AzResource -ResourceGroupName joywebapp -Name $name -ResourceType microsoft.insights/metricAlerts | ConvertTo-Json
}

enter image description here

Upvotes: 1

Bhargavi Annadevara
Bhargavi Annadevara

Reputation: 5512

Joy is right in the way that the cmdlet should still execute as what you see is just a warning. However, this could be happening as Powershell support for newer metric alerts is still in the works as mentioned in the Official docs.

Also, as an alternative, if it helps, you could use Azure CLI to list newer Metric Alerts, as it now supports fetching elaborate results of queries belonging to the Microsoft.Insights/metricAlerts resource type.

For example:

az monitor metrics alert list -g <Resource group name> --output yaml

The result would look something like this:

Az CLI get Metric Alert

You also get to choose out of the many output formats (json, jsonc, yaml, table, tsv) available with Az CLI.

Hope this helps!

Upvotes: 0

Related Questions