Az.Monitor PowerShell Module

Overview

The Az.Monitor module contains cmdlets for managing Azure Monitor resources, including metrics, alerts, log analytics, and diagnostics settings.

Installation

Install-Module -Name Az.Monitor -Repository PSGallery -Force

Update the module with:

Update-Module -Name Az.Monitor

Cmdlet Reference

CmdletPurpose
Get-AzMetricRetrieve metrics for a resource.
New-AzMetricAlertRuleV2Create a metric alert rule (v2).
Remove-AzMetricAlertRuleV2Delete a metric alert rule.
Get-AzLogAnalyticsWorkspaceList Log Analytics workspaces.
New-AzLogAnalyticsWorkspaceCreate a new Log Analytics workspace.
Set-AzDiagnosticSettingConfigure diagnostic settings for a resource.
Remove-AzDiagnosticSettingRemove diagnostic settings.
Get-AzActivityLogQuery activity logs.
New-AzActionGroupCreate an action group for alerts.
Remove-AzActionGroupDelete an existing action group.

Usage Examples

1. Retrieve CPU usage metrics for a virtual machine

Get-AzMetric -ResourceId "/subscriptions/xxxx/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/vm1" `
    -MetricName "Percentage CPU" -TimeGrain "PT1M" -StartTime (Get-Date).AddHours(-1) -EndTime (Get-Date)

2. Create a metric alert for high CPU usage

$criteria = New-AzMetricAlertRuleV2Criteria -MetricName "Percentage CPU" -TimeAggregation "Average" -Operator "GreaterThan" -Threshold 80
New-AzMetricAlertRuleV2 -Name "HighCpuAlert" -ResourceGroupName "rg1" -Location "EastUS" `
    -TargetResourceId "/subscriptions/xxxx/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/vm1" `
    -WindowSize (New-TimeSpan -Minutes 5) -Frequency (New-TimeSpan -Minutes 1) -Condition $criteria `
    -ActionGroupId "/subscriptions/xxxx/resourceGroups/rg1/providers/microsoft.insights/actionGroups/MyGroup"

3. Configure diagnostics to send logs to a Log Analytics workspace

$workspace = Get-AzLogAnalyticsWorkspace -ResourceGroupName "rg1" -Name "myWorkspace"
Set-AzDiagnosticSetting -ResourceId "/subscriptions/xxxx/resourceGroups/rg1/providers/Microsoft.Storage/storageAccounts/storage1" `
    -WorkspaceId $workspace.ResourceId -Enabled $true -Logs @(
        @{category="StorageRead"; enabled=$true}
        @{category="StorageWrite"; enabled=$true}
    )