Question:
Simple question, trying to use Powershell Azure ARM to Disable/Enable my Application Insights Availability Tests. We have scheduled reboots of our servers, and I want to black out those times so we don’t record errors.
I tried the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$alert = Get-AzureRmAlertRule -ResourceGroupName $resourceGroup ` | Where-Object {$_.Name -like '*isalive*'} ` | Where-Object {$_.Name -like "*$vmName*"} Add-AzureRmWebtestAlertRule -Name $alert.Name ` -Location $alert.Location ` -ResourceGroupName $resourceGroup ` -WindowSize $alert.Condition.WindowSize ` -MetricName $alert.Condition.DataSource.MetricName ` -TargetResourceUri $alert.Id ` -FailedLocationCount $alert.Condition.FailedLocationCount ` -DisableRule ` -Verbose |
But I get Back
WARNING: 3:34:18 PM – The namespace for all the model classes will
change from Microsoft.Azure.Management.Monitor.Management.Models to
Microsoft.Azure.Management.Monitor.Models in future releases.WARNING: 3:34:18 PM – The namespace for output classes will be uniform for all classes in future releases to make it independent of
modifications in the model classes.VERBOSE: Performing the operation “Create/update an alert rule” on target “Create/update an alert rule: azecdag02 isalive-sitecore
production-2c06a496-3567-4871-a57c-2c516c0ccfef from resource group:
OAT_Website”.VERBOSE: 3:34:18 PM – CreateRuleCondition: Creating location threshold rule condition (webtest rule)
VERBOSE: 3:34:18 PM – CreateSdkCallParameters: Creating rule object
Add-AzureRmWebtestAlertRule : Exception type: ErrorResponseException, Message: The setting already exists., Code:
SettingAlreadyExists, Status code:Conflict, Reason phrase: Conflict
At line:1 char:1
+ Add-AzureRmWebtestAlertRule
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Add-AzureRmWebtestAlertRule], PSInvalidOperationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmWebtestAlertRuleCommand
And if I try Resolve-AzureRmError -Last
I get
HistoryId: 80
Message : Exception type: ErrorResponseException, Message: The
setting already exists., Code: SettingAlreadyExists, Status
code:Conflict, Reason phrase: Conflict StackTrace : at
Microsoft.Azure.Commands.Insights.MonitorCmdletBase.ExecuteCmdlet()
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
Exception :
System.Management.Automation.PSInvalidOperationException
InvocationInfo : {Add-AzureRmWebtestAlertRule} Line :
Add-AzureRmWebtestAlertRule
Position : At line:1 char:1
+ Add-AzureRmWebtestAlertRule
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HistoryId : 80Message : Operation returned an invalid status code ‘Conflict’
StackTrace : at
Microsoft.Azure.Management.Monitor.AlertRulesOperations.d__5.MoveNext()
Exception :
Microsoft.Azure.Management.Monitor.Models.ErrorResponseException
InvocationInfo : {Add-AzureRmWebtestAlertRule} Line :
Add-AzureRmWebtestAlertRule `
Position : At line:1 char:1
+ Add-AzureRmWebtestAlertRule
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HistoryId : 80
Answer:
I finally was able to get this working, see the below script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#later versions of AzureRM don't support Find #Get-AzureRmResources -ResourceGroupName "My_Website" $allAlerts = Find-AzureRmResource -ResourceGroupNameEquals "OAT_Website" ` | Where-Object -Property ResourceType -EQ "microsoft.insights/webtests" ; $vmPrefix = "azesc" $color = "blue" $status = "True" $alertIDs = $allAlerts | Where-Object {$_.Name -like "*$vmPrefix*" -or $_.Name -like "*$color*" } ` | Select-Object -ExpandProperty ResourceId ForEach ($alertID in $alertIDs) { $alert = Get-AzureRMResource -ResourceId $alertID $alert.Properties.Enabled = $status $alert | Set-AzureRMResource -Force } $vmPrefix = "azec" $color = "green" $status = "False" $alertIDs = $allAlerts | Where-Object {$_.Name -like "*$vmPrefix*" -or $_.Name -like "*$color*" } ` | Select-Object -ExpandProperty ResourceId ForEach ($alertID in $alertIDs) { $alert = Get-AzureRMResource -ResourceId $alertID $alert.Properties.Enabled = $status $alert | Set-AzureRMResource -Force } |