SOLVED

Disable/Enable Azure monitor alert rules by azure automation powershell

Copper Contributor

Hi,

 

I have an azure monitoring alert rule where it will check the heartbeat of my virtual machine for every 5 minutes. But, the virtual machine itself will go offline everyday at 11 PM and will be started again on 9 AM on next day.

so I'm trying to use azure automation to disable/enable my alert rule at the same time. this is the code I've tried to use:

Write-Output "start job"
$vmResourceGroupName = <<resource_group>>
$vmName = <<vm_name>>

try
{
# Connection
Write-Output "connect to the VM"
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
$rcConn = Connect-AzAccount -ServicePrincipal -TenantId $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

# Stop the VM
Write-Output "Stop the VM"
stop-AzVM -ResourceGroupName $vmResourceGroupName -Name $vmName -Force
#first method that I use to disable my alert rules
Get-AzAlertRule -ResourceGroupName $vmResourceGroupName -TargetResourceId <<my_resource_id>> -DisableRule
#second method that I use to disable my alert rules
Disable-AzureRmActivityLogAlert -Name <<my_alert_name>> -ResourceGroupName <<my_resource_group>>
}
catch
{
if($_.Exception.Message)
{
Write-Error -Message "$($_.Exception.Message)" -ErrorAction Continue
}
else
{
Write-Error -Message "$($_.Exception)" -ErrorAction Continue
}
throw "$($_.Exception)"
}
finally
{
Write-Output "end job"
}


 

both of the method which I use returning an error that said my alert rules are not found.

2 Replies
best response confirmed by orobmontana (Copper Contributor)
Solution

hi, solved this problem.

 

created thread for this problem also on Stack Overflow, please feel free to see the answer there :

https://stackoverflow.com/questions/57372231/disable-enable-azure-monitor-alert-rules-by-azure-autom...

1 best response

Accepted Solutions
best response confirmed by orobmontana (Copper Contributor)
Solution

hi, solved this problem.

 

created thread for this problem also on Stack Overflow, please feel free to see the answer there :

https://stackoverflow.com/questions/57372231/disable-enable-azure-monitor-alert-rules-by-azure-autom...

View solution in original post