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
| Cmdlet | Purpose |
|---|---|
Get-AzMetric | Retrieve metrics for a resource. |
New-AzMetricAlertRuleV2 | Create a metric alert rule (v2). |
Remove-AzMetricAlertRuleV2 | Delete a metric alert rule. |
Get-AzLogAnalyticsWorkspace | List Log Analytics workspaces. |
New-AzLogAnalyticsWorkspace | Create a new Log Analytics workspace. |
Set-AzDiagnosticSetting | Configure diagnostic settings for a resource. |
Remove-AzDiagnosticSetting | Remove diagnostic settings. |
Get-AzActivityLog | Query activity logs. |
New-AzActionGroup | Create an action group for alerts. |
Remove-AzActionGroup | Delete 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}
)