Users: 30k; Pages Per Session: 2.5p; Avg. Session Duration: 1:35mi
TechDevOps.com
Explore Tools
{Featuring Microsoft Technologies + Enterprise Data + Cloud Computing}



Powershell Azure Virtual Machine Alerts with Webhook & Logic App
by BF (Senior MSSQL Operations - Enterprise, Cloud, Strategy)
2016-09-06








Using Powershell Code to provision new objects in Microsoft Azure Cloud is a advantage to any CloudOps. Some of the key advantages are:

- Quickly deploy modifications to existing objects in Microsoft Azure
- Recreate identical environments in Development, Testing, Staging & Production
- Source Control the Powershell Code for Source Code Management & Version Control
- No need to document any GUI implementations

Below is the complete Powershell Code to provision new Virtual Machine Alerts and configure a Webhook for those Alerts. It will connect
to your Azure Subscription and loop through each Resource Group and Virtual Machine and provision new Alerts. It allows for complete
deletion and also a view of the possible Alerts configurable.

Note: The Webhook connects to a Logic App for custom alerting but that is outside the scope of this article. Check here for details on the Logic App.



Powershell Code:

#Authenticate to Azure
#Login-AzureRmAccount

#Set Variables:
$Subscription = "DEVELOPMENT"
$OperatorEmails = 'alerts@xyzops.com'
$DeleteNewMetricAlertRules = "0" #1=Delete
$ShowMetricOptionsAndExit = "0" #1=Show
$CheckGetAzureRmAlertRule = "0" #1=Show
$ValidVMNames = 'VM01', 'VM02'

If ($Subscription -eq "DEVELOPMENT")
{

Write-Host '>Deploying to Azure DEVELOPMENT Subscription...'

#Select your Azure Subscription
Select-AzureRmSubscription -SubscriptionName "DEVELOPMENT"

#Define Actions Email
$AlertEmail = New-AzureRmAlertRuleEmail -CustomEmails $OperatorEmails

#Define Actions Webhook
$WebhookURI = New-AzureRmAlertRuleWebhook -ServiceUri 'https://'

}

If ($Subscription -eq "TESTING")
{
Write-Host 'Deploying to Azure TESTING Subscription...'

#Select your Azure Subscription
Select-AzureRmSubscription -SubscriptionName "TESTING"

#Define Actions Email
$AlertEmail = New-AzureRmAlertRuleEmail -CustomEmails $OperatorEmails

#Define Actions Webhook
$WebhookURI = New-AzureRmAlertRuleWebhook -ServiceUri 'https://'

}

If ($Subscription -eq "PRODUCTION")
{
Write-Host 'Deploying to Azure PRODUCTION Subscription...'

#Select your Azure Subscription
Select-AzureRmSubscription -SubscriptionName "PRODUCTION"

#Define Actions Email
$AlertEmail = New-AzureRmAlertRuleEmail -CustomEmails $OperatorEmails

#Define Actions Webhook
$WebhookURI = New-AzureRmAlertRuleWebhook -ServiceUri 'https://'
}


#Get all Resource Groups
$RGs = Get-AzureRMResourceGroup | Select-Object -Property ResourceGroupName | where {$_.ResourceGroupName -like '*SQL*'}

#Loop through all Resource Groups
foreach($RG in $RGs)
{

#Get all Virtual Machines
$VMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName


#Loop through all Virtual Machines
foreach($VM in $VMs)
{

$Server=$VM.Name
$Location = $VM.Location
$ResourceGroup = $RG.ResourceGroupName

If ($ValidVMNames -like '*' + $Server + '*')
{

Write-Host '>Updating...' $ResourceGroup
Write-Host '>Updating...' $Server

If ($DeleteNewMetricAlertRules -eq "1")
{
#Remove New Metric Alert Rule
Write-Host '>Deleting New Metric Alert Rule(s) request sent..'

Remove-AzureRmAlertRule -ResourceGroup $ResourceGroup -Name 'CPU'
Remove-AzureRmAlertRule -ResourceGroup $ResourceGroup -Name 'MEM'

Write-Host '>Azure New Metric Rules(s) deleted = ok'
}
Else
{
Write-Host '>Azure VM New Metric Alert Rule(s) request sent..'

#Get Azure ResourceID
$ResourceID = (Get-AzureRmResource -ResourceGroupName $ResourceGroup -ResourceName "$Server" -ResourceType "Microsoft.Compute/virtualMachines").ResourceID

If ($ShowMetricOptionsAndExit -eq "1")
{
Write-Host '>Exiting with ShowMetricOptions..'
Get-AzureRmMetricDefinition -ResourceId $ResourceID | Format-Table
Exit
}

#Create New Metric Alert Rule
Write-Host '>Azure New Metric Alert Rule(s) request sent..'

Add-AzureRmMetricAlertRule -Name 'CPU' -ResourceGroup $ResourceGroup -Location $Location -TargetResourceId $ResourceID -MetricName "\Processor(_Total)\% User Time" -Operator GreaterThan -Threshold 90 -WindowSize 00:05:00 -TimeAggregationOperator Maximum -Actions $AlertEmail,$WebhookURI

Add-AzureRmMetricAlertRule -Name 'MEM' -ResourceGroup $ResourceGroup -Location $Location -TargetResourceId $ResourceID -MetricName "\Memory\Available Bytes" -Operator LessThan -Threshold 1000000000 -WindowSize 00:05:00 -TimeAggregationOperator Maximum -Actions $AlertEmail,$WebhookURI

#$AlertRule = Get-AzureRmAlertRule -ResourceGroup $ResourceGroup | Select-Object -Property Name | Format-List
#$AlertRule

Write-Host '>Azure VM New Metric Rules(s) created = ok'

If ($CheckGetAzureRmAlertRule -eq "1")
{
Write-Host '>Check Get-AzureRmAlertRule request sent..'

Get-AzureRmAlertRule -Name 'CPU' -ResourceGroup $ResourceGroup -DetailedOutput
}
}
}

}
}







If you receive this error message when running the Powershell Code "Alert rule is not available for this resource type”, this means you need
to enable & set Diagnostics on the Virtual Machine.



Resources:

Microsoft Azure

Logic Apps

What are Logic Apps?

Azure Automation webhooks

How to configure webhooks for alerts