我需要创建多个天蓝色监视器警报。我正在尝试在此网站之后执行此操作: https ://www.azureblue.io/how-to-create-an-alert-rule-using-powershell-and-azure-cli/
如果我在同一个订阅中有操作组和目标资源,则此解决方案适用于我,但通常我没有。我在近 100 个订阅中拥有一个行动组和数百个目标。
我有:
#jump to subscription with actiongroup
$context = Get-AzSubscription -SubscriptionId xxx
Set-AzContext $context
$actionGroup = Get-AzActionGroup -name "xxx" -ResourceGroupName "xxx"
$actionGroupId = New-AzActionGroup -ActionGroupId $actionGroup.Id
#jump to subscription with target without this I can't ask for target.id
$context = Get-AzSubscription -SubscriptionId xx
Set-AzContext $context
# Creates a local criteria object that can be used to create a new metric alert
$condition = New-AzMetricAlertRuleV2Criteria `
-MetricName "Data space used percent (Platform)" `
-TimeAggregation Maximum `
-Operator GreaterThan `
-Threshold 0.8
$windowSize = New-TimeSpan -Minutes 30
$frequency = New-TimeSpan -Minutes 5
$targetResourceId = (Get-AzResource -Name xxx).Id
#I was thinking that this jump to subscription will solve my issue but doesn't
$context = Get-AzSubscription -SubscriptionId cf653672-c304-49a2-b01b-171f6236bad6
Set-AzContext $context
# Adds or updates a V2 (non-classic) metric-based alert rule.
Add-AzMetricAlertRuleV2 `
-Name "test" `
-ResourceGroupName "xxx" `
-WindowSize $windowSize `
-Frequency $frequency `
-TargetResourceId $targetResourceId `
-Condition $condition `
-ActionGroup $actionGroupId `
-Severity 3
我收到了这个错误:
Add-AzMetricAlertRuleV2 : Exception type: ErrorResponseException, Message: Null/Empty, Code: Null, Status code:NotFound, Reason phrase: Not Found
At line:26 char:1
+ Add-AzMetricAlertRuleV2 `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Add-AzMetricAlertRuleV2], PSInvalidOperationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmMetricAlertRuleV2Command
从 GUI 中选择不同订阅的操作组没有问题,但您不能从多个订阅中选择目标。
如果我能找到这个示例的解决方案,我将尝试阅读我的所有资源并循环运行代码。