{"id":623,"date":"2022-07-15T03:09:45","date_gmt":"2022-07-15T03:09:45","guid":{"rendered":"http:\/\/draith.com\/?p=623"},"modified":"2023-01-04T03:12:07","modified_gmt":"2023-01-04T03:12:07","slug":"adding-azure-alert-rules-with-powershell-7","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=623","title":{"rendered":"Adding Azure Alert Rules with PowerShell 7"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Here is a quick and dirty post to create both Metric and Scheduled Query alert rule types in Azure:<\/p>\n\n\n\n<p>To create an Azure Alert rule using Powershell 7 and the AZ module, you will need to install both Powershell 7 and the AZ module.<\/p>\n\n\n\n<p>PowerShell 7: <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/install\/installing-powershell?view=powershell-7.1\">https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/install\/installing-powershell?view=powershell-7.1<\/a><\/p>\n\n\n\n<p>To install the AZ module, run the following command in Powershell:<\/p>\n\n\n\n<p><code>Install-Module -Name AZ<\/code><\/p>\n\n\n\n<p>Once both Powershell 7 and the AZ module are installed, you can use the following commands to create an Azure Alert rule.<\/p>\n\n\n\n<p>To create a metric alert rule:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$alertRule = New-AzMetricAlertRule `\n    -ResourceGroupName &quot;MyResourceGroup&quot; `\n    -RuleName &quot;MyMetricAlertRule&quot; `\n    -TargetResourceId &quot;\/subscriptions\/{subscription-id}\/resourceGroups\/{resource-group-name}\/providers\/Microsoft.Compute\/virtualMachines\/{vm-name}&quot; `\n    -MetricName &quot;Percentage CPU&quot; `\n    -Operator GreaterThan `\n    -Threshold 90 `\n    -WindowSize 30 `\n    -TimeAggregationOperator Average\n<\/pre><\/div>\n\n\n\n<p>And to create a Scheduled Query rule:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$alertRule = New-AzLogAlertRule `\n    -ResourceGroupName &quot;MyResourceGroup&quot; `\n    -RuleName &quot;MyScheduledQueryAlertRule&quot; `\n    -Location &quot;East US&quot; `\n    -Condition &quot;AzureMetrics | where ResourceProvider == 'Microsoft.Compute' and ResourceType == 'virtualMachines' and MetricName == 'Percentage CPU' and TimeGrain == 'PT1H' and TimeGenerated &gt; ago(2h) | summarize AggregateValue=avg(MetricValue) by bin(TimeGenerated, 1h), ResourceId&quot; `\n    -ActionGroupId &quot;\/subscriptions\/{subscription-id}\/resourceGroups\/{resource-group-name}\/providers\/Microsoft.Insights\/actionGroups\/{action-group-name}&quot;\n<\/pre><\/div>\n\n\n\n<p>You will have to replace the main bits you would expect &#8211; ResourceGroupName, Subscription-ID, Action-Group-Name, Location, etc.  <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hope this helps! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a quick and dirty post to create both Metric and Scheduled Query alert rule types in Azure: To create an Azure Alert rule using Powershell 7 and the AZ module, you will need to install both Powershell 7 and the AZ module. PowerShell 7: https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/install\/installing-powershell?view=powershell-7.1 To install the AZ module, run the following &hellip; <a href=\"https:\/\/draith.com\/?p=623\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Adding Azure Alert Rules with PowerShell 7&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,16],"class_list":["post-623","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-azure","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=623"}],"version-history":[{"count":1,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/623\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/623\/revisions\/624"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}