简体   繁体   English

如何使用 PowerShell 在 Azure 中创建新的警报规则?

[英]How to create a new alert rule in Azure using PowerShell?

I am going nuts here.我要疯了。 How in the world can I create a new alert rule in Azure using PowerShell?在世界上如何使用 PowerShell 在 Azure 中创建新的警报规则?

With Azure CLI it's as easy as this使用 Azure CLI 就这么简单

# Retrieve id for scope parameter
az vm show -n vm1 -o tsv --query id 

# Create action group
az monitor action-group create -n action-group1 -g rg-training --action email admin adminaddress@host.org

# Create alert
az monitor metrics alert create -n alert2 -g rg-training --scopes "/subscriptions/<guid>/resourceGroups/rg-training/providers/Microsoft.Compute/virtualMachines/vm1" --condition "avg Percentage CPU > 90" --window-size 5m --evaluation-frequency 1m --action action-group1 --description "High CPU"

This is what my PowerShell attempts look like这就是我的 PowerShell 尝试的样子

Import-Module Az.Monitor

# Create a new condition
$condition = New-AzMetricAlertRuleV2Criteria -MetricName "Percentage CPU" -MetricNameSpace "Microsoft.Compute/virtualMachines" -TimeAggregation Average -Operator GreaterThan -Threshold 5

# Create new action group and receiver
$receiver = New-AzActionGroupReceiver -Name "Admin" -EmailReceiver -EmailAddress "admin@host.org"
Set-AzActionGroup -Name "my-action-group" -ShortName "ActionGroup1" -ResourceGroupName "rg-training" -Receiver $receiver

# Retrieve resource id from vm1
$resourceID = get-azresource -name vm1 | Select-Object resourceid

# Finally add the alert rule that will trigger when for more then 5 min CPU percentage is more then 10%
Add-AzMetricAlertRuleV2 -Name "metricRule2" -ResourceGroupName "rg-training" -WindowSize 00:05:00 -Frequency 00:01:00 -TargetResourceId $resourceID -Condition $condition -ActionGroupId "my-action-group" -Severity 4

Which results in这导致

> Add-AzMetricAlertRuleV2 : Exception type: ErrorResponseException,
> Message: Null/Empty, Code: Null, Status code:BadRequest, Reason
> phrase: Bad Request At C:\Temp\03 Create and test alerts.ps1:17 char:1
> + Add-AzMetricAlertRuleV2 -Name "metricRule2" -ResourceGroupName "rg-tr ...
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo          : CloseError: (:) [Add-AzMetricAlertRuleV2], PSInvalidOperationException
> + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmMetricAlertRuleV2Command

What am I doing wrong here?我在这里做错了什么?

I have figured it out in the meantime... the culprit was the way I retrieved $targetResourceId .与此同时,我已经弄清楚了……罪魁祸首是我检索$targetResourceId的方式。 The following worked for me...以下对我有用...

# TypeName: Microsoft.Azure.Commands.Insights.OutputClasses.PSActionGroupResource
$actionGroup = Get-AzActionGroup -Name "my-action-group" -ResourceGroupName "rg-training" 

# TypeName: Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup
$actionGroupId = New-AzActionGroup -ActionGroupId $actionGroup.Id

# TypeName: System.TimeSpan
$windowSize = New-TimeSpan -Minutes 1

# TypeName: System.TimeSpan
$frequency = New-TimeSpan -Minutes 1 

# TypeName: Microsoft.Azure.Commands.Insights.OutputClasses.PSMetricCriteria
$condition = New-AzMetricAlertRuleV2Criteria -MetricName "Percentage CPU" -TimeAggregation Average -Operator GreaterThan -Threshold 0.1

# TypeName: System.String
$targetResourceId = (Get-AzResource -Name VM1).ResourceId

# TypeName: Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext
$context = Get-AzContext

Add-AzMetricAlertRuleV2 -Name "metricRule" -ResourceGroupName "rg-training" -WindowSize $windowSize -Frequency $frequency -TargetResourceId $targetResourceId -Condition $condition -ActionGroup $actionGroupId -Severity 3

You can read more about it here你可以在这里阅读更多关于它的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Log Analytics 工作区警报规则在使用 Azure powershell 禁用警报规则时给出错误网关错误 - Azure Log Analytics workspace alert rule giving bad gateway error while disabling alert rule using Azure powershell 如何使用Azure API检索“日志搜索”警报规则 - How to retrieve a “Log Search” alert rule using the Azure API Azure 使用 Powershell 的成本警报 - Azure Cost Alert using Powershell 如何使用Powershell在Azure中检索警报的当前状态 - How to retrieve the current state of an Alert in Azure using Powershell Azure 监视器 - 创建新订阅时如何创建警报? - Azure Monitor - How to create an alert when a new subscription is created? 如何使用Powershell创建Maximum Azure VM - How to create Maximum azure VMs using powershell 使用 Powershell 修改 azure nsg 规则 - Using Powershell to modify azure nsg rule 如何使用Azure PowerShell创建PasswordCredentials对象并传递New-AzADApplication - How to create PasswordCredentials object and pass in New-AzADApplication using Azure PowerShell 如何使用Azure Powershell创建Azure App服务? - How to create azure app service using azure powershell? 通过Powershell创建日志警报规则,并将多个操作组附加到该规则 - Create log alert rule through powershell and attach more than one action groups to that rule
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM