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

标签 azure powershell azure-powershell azure-cli

我快要疯了。我究竟如何使用 PowerShell 在 Azure 中创建新的警报规则?

使用 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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7a7f7672757a7f7f697e68685b7374686f3574697c" rel="noreferrer noopener nofollow">[email protected]</a>

# 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"

这就是我的 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 "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="650401080c0b250d0a16114b0a1702" rel="noreferrer noopener nofollow">[email protected]</a>"
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

结果

> 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

我在这里做错了什么?

最佳答案

我同时已经弄清楚了......罪魁祸首是我检索$targetResourceId的方式。以下内容对我有用......

# 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

您可以阅读更多相关信息 here

关于azure - 如何使用 PowerShell 在 Azure 中创建新的警报规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61702023/

相关文章:

azure - 如何仅获取 Azure key 保管库的部分 Powershell 输出?

windows - 是否可以使用登录的 Windows 用户凭据对 Web API 进行身份验证?

ios - 发送通知时 Azure 通知中心注册消失

powershell - 使用 Import-Excel 导入数字然后使用 PSObjects 作为数字的正确方法是什么?

powershell - 列出属于特定子网的所有 azure vm

azure - Powershell - 删除容器中的所有 Blob

c# - 从azure openai sdk api返回流的正确方法

azure - Web 部署任务失败。 ERROR_USER_UNAUTHORIZED

powershell - 使用 powershell 启动/停止 IIS 网站时的权限

powershell - 当嵌套在点源调用中时,如何获取调用类的脚本文件的名称