如何使用 foreach 循环通过 powershell 为资源组中的所有数据库创建警报规则

How to create alert rule for all DBs in a resource group via powershell using foreach loop

我正在寻找创建 DTU 警报规则,以监视最后一个 15min.I 超过 90% 的 DTU 想要对资源组中的所有数据库执行此操作。 这个想法是自动化工作并在 GUI 中保存数十条规则的手动创建,同时避免 运行 每个脚本一个脚本 alert.It 基本上必须为尽可能多的数据库创建相同的警报在资源组中有但提供唯一名称,请参阅脚本中的 "THAT MUST BE UNIQUE" 部分

我写的脚本是:

#define variable for resource group name by requesting keynoard input

$rg = Read-Host 'Please, input resource group name here (exactly as it is in Azure)'

<#create the array containing databases where alerts are required. The value of v12.0,user corresponds to the kind of resource as to include only the SQL DBs and not the SQL servers#>

$resources = Get-AzureRmResource | ?{ $_.ResourceGroupName -eq $rg -and $_.kind -eq "v12.0,user"  } | select -expandpropert resourceid

#loop through the array and create the alert rule for each DB

foreach($resource in $resources){Add-AzureRMMetricAlertRule -ResourceGroup $rg -location "Central US" -targetresourceid $resource -Name "THAT MUST BE UNIQUE" -MetricName "dtu_consumption_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "putemail@here.com")}

问题是它只创建了一个警报,然后出现以下错误(这可能指向 -name 值不唯一的问题):

Add-AzureRMMetricAlertRule : Exception type: ErrorResponseException, Message: Can not update target resource id during
update., Code: BadRequest, Status code:BadRequest, Reason phrase: Bad Request
At C:\Users\CreateDTUalertsFORallDBv2.ps1:11 char:34
+ ... $resources){Add-AzureRMMetricAlertRule -ResourceGroup $rg -location " ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Add-AzureRmMetricAlertRule], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmMetricAlertRuleCommand

你能告诉我出了什么问题吗?我如何才能按照脚本中的参数为资源组中的每个数据库创建 DTU 指标?我也很好奇如何填充“ -name”来自上述脚本的参数,其中包含警报将起作用的数据库所独有的内容(理想情况下,资源名称值可以从脚本中 foreach 循环之前的 Get-AzureRmResource commandlet 提供).

如果我尝试使用以下脚本使用数据库的 RESOURCENAME 填充 -name 参数:

#define variable for resource group name by requesting keynoard input

$rg = Read-Host 'Please, input resource group name here (exactly as it is in Azure)'

#create the array containing databases where alerts are required

$resources = Get-AzureRmResource | ?{ $_.ResourceGroupName -eq $rg -and $_.kind -eq "v12.0,user"  } | select -expandpropert resourceid

#loop through the array and create the alert rule for each DB

foreach($resource in $resources){$resourcename = (Get-AzureRmResource -ResourceGroupName $rg -Resourceid $resource).resourcename;Add-AzureRMMetricAlertRule -ResourceGroup $rg -location "Central US" -targetresourceid $resource -Name $resourcename -MetricName "dtu_consumption_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "Client-DestinationHotels@hhogdev.com")}

关于名称不唯一的错误,请参阅以下错误:

    Add-AzureRmMetricAlertRule : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\Users\ttest.ps1:11 char:234
+ ...  "Central US" -targetresourceid $resource -Name $resourcename -Metric ...
+                                                     ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-AzureRmMetricAlertRule], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmMetr
   icAlertRuleCommand

Get-AzureRmResource : Parameter set cannot be resolved using the specified named parameters.
At C:\Users\ttest.ps1:11 char:51
+ ... urcename = (Get-AzureRmResource -ResourceGroupName $rg -Resourceid $r ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-AzureRmResource], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Ge

您尝试创建的确切 DTU 警报已在 this 文章中逐步创建。

好的,我是这样做的。我不得不动态传递资源名称,但在第一个斜杠后将其剪切(资源名称值在字符串中有一个斜杠,这是有问题的)

#define variable for resource group name by requesting keyboard input

$rg = Read-Host 'Please, input resource group name here (exactly as it is in Azure)'

<#create the array containing databases where alerts are required. The value of v12.0,user corresponds to the kind of resource as to include only the SQL DBs and not the SQL servers#>

$resources = Get-AzureRmResource | ?{ $_.ResourceGroupName -eq $rg -and $_.kind -eq "v12.0,user"  } | select resourcename,resourceid

#loop through the array and create the alert rule for each DB

foreach($resource in $resources){$alertname=$resource.resourcename.Substring($resource.resourcename.IndexOf('/')+1);Add-AzureRMMetricAlertRule -ResourceGroup $rg -location "centralus" -targetresourceid $resource.resourceid -Name $alertname -MetricName "dtu_consumption_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "Client-address@domain.com")}