We're Hiring!
Take the next step in your career and work on diverse technology projects with cross-functional teams.
LEARN MORE
Mountain West Farm Bureau Insurance
office workers empowered by business technology solutions
BLOG
3
14
2019

Automating Azure Alert Rule Creation Using PowerShell and ARM Templates

Last updated:
10.15.2020

Alert Rules in Azure are a tool to let you know when some condition of your choice has occurred within any given component of your Azure infrastructure. In other words, they alert you to potential problems so you can remedy them before anything serious goes wrong.

Have you ever had the tedious task of creating multiple alerts for all of the resources in your subscription? Let me tell you, it is really time consuming to create them from scratch one by one.

I have a PowerShell Script that can Target and Create specific metric alerts for the resources you define inside of the script, making it much simpler to create a large amount of alerts at one time.

Skip down to the script if you’re familiar with Alerts already. If you aren’t here’s an overview on how they work.
 

An Intro to Azure Alerts

Alert Rules define the target resource that will be monitored, subject that resource to a logic test when a signal is sent, and then pass it on to either an action group, with associated actions to take, or an alert state.

The target resource can be anything within your Azure environment: VMs, storage, scale sets, Log Analytics workspaces, or Application Insights. You can also select multiple resources as a single rule target.

Alert Rules are highly configurable so you can set up a wide variety of Criteria, designate the Severity from 0 to 4, and set up Action Groups accordingly. Criteria is a combination of the signal from the resource and logic, such as a certain % threshold on your CPU or a long server response time.

So, to set up a single Alert Rule, you’ll have to pick the target, select an available signal, and specify the logic that will be used for the signal data. Then you’ll have to either associate it with an Action Group or a Monitor condition. Time consuming, to be sure.

 

Show Me the Automation Script Already!

First make sure you have imported the following modules into PowerShell:

Next, create two JSON files, one will be parameters.json and the other template.json. Follow those links to download the complete json files via Github.

Place them in a local folder:

Now let's take a look at the script itself. Follow this link to download it from Github or click on the image below.

PLEASE NOTE: this portion of the code targets only Virtual Machines in its current state. 

If you want to target additional resource types, follow this link to view all the supported metrics for Azure Monitor. Choose the resource type and metric and make the desired changes inside of the script as indicated below.

Change this section to target a different resource type and metric:

Now let’s see the progress in real time!

After the script finishes go to the Deployments section of the resource group you defined, you will see each individual job being deployed:

Creating Azure Alert Rules this way will save you lots of time. This code is highly flexible and can be easily modified. If you would like a more complex alert, simply change the parameters and template JSON, update the new values in the script, and fire away!

Recent Blog Posts

lunavi logo alternate white and yellow
2.11.2025
2
.
7
.
2025
The Future of Test Automation: Key Trends Shaping 2025 and Beyond

Software testing has gone from a chore to a game-changer, thanks to automation. But in 2025, sticking to old methods means falling behind. Stay ahead by embracing the future of test automation—let’s explore the key trends shaping what’s next.

Learn more
lunavi logo alternate white and yellow
2.11.2025
1
.
23
.
2025
The Importance of Cross Browser Testing

Making sure users have a smooth experience across all these platforms is crucial for businesses to stay competitive. Cross-browser testing is now a key part of modern development. It helps teams find and fix problems like layout issues, broken features, or slow performance before users are affected. Let’s look at why cross-browser testing matters and explore tools that make it easier to get the job done.

Learn more
lunavi logo alternate white and yellow
11.19.2024
11
.
8
.
2024
Load & Performance Testing with Azure Load Testing Service

Learn about load and performance testing in Microsoft Azure.

Learn more