SOLVED

Disable/Enable Azure monitor alert rules by azure automation powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-789673%22%20slang%3D%22en-US%22%3EDisable%2FEnable%20Azure%20monitor%20alert%20rules%20by%20azure%20automation%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-789673%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20an%20azure%20monitoring%20alert%20rule%20where%20it%20will%20check%20the%20heartbeat%20of%20my%20virtual%20machine%20for%20every%205%20minutes.%20But%2C%20the%20virtual%20machine%20itself%20will%20go%20offline%20everyday%20at%2011%20PM%20and%20will%20be%20started%20again%20on%209%20AM%20on%20next%20day.%3C%2FP%3E%3CP%3Eso%20I'm%20trying%20to%20use%20azure%20automation%20to%20disable%2Fenable%20my%20alert%20rule%20at%20the%20same%20time.%20this%20is%20the%20code%20I've%20tried%20to%20use%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3EWrite-Output%20%22start%20job%22%0A%24vmResourceGroupName%20%3D%20%26lt%3B%3CRESOURCE_GROUP%3E%26gt%3B%0A%24vmName%20%3D%20%26lt%3B%3CVM_NAME%3E%26gt%3B%0A%0Atry%0A%7B%0A%23%20Connection%0AWrite-Output%20%22connect%20to%20the%20VM%22%0A%24Conn%20%3D%20Get-AutomationConnection%20-Name%20AzureRunAsConnection%0A%24rcConn%20%3D%20Connect-AzAccount%20-ServicePrincipal%20-TenantId%20%24Conn.TenantID%20-ApplicationId%20%24Conn.ApplicationID%20-CertificateThumbprint%20%24Conn.CertificateThumbprint%0A%0A%23%20Stop%20the%20VM%0AWrite-Output%20%22Stop%20the%20VM%22%0Astop-AzVM%20-ResourceGroupName%20%24vmResourceGroupName%20-Name%20%24vmName%20-Force%0A%23first%20method%20that%20I%20use%20to%20disable%20my%20alert%20rules%0AGet-AzAlertRule%20-ResourceGroupName%20%24vmResourceGroupName%20-TargetResourceId%20%26lt%3B%3CMY_RESOURCE_ID%3E%26gt%3B%20-DisableRule%0A%23second%20method%20that%20I%20use%20to%20disable%20my%20alert%20rules%0ADisable-AzureRmActivityLogAlert%20-Name%20%26lt%3B%3CMY_ALERT_NAME%3E%26gt%3B%20-ResourceGroupName%20%26lt%3B%3CMY_RESOURCE_GROUP%3E%26gt%3B%0A%7D%0Acatch%0A%7B%0Aif(%24_.Exception.Message)%0A%7B%0AWrite-Error%20-Message%20%22%24(%24_.Exception.Message)%22%20-ErrorAction%20Continue%0A%7D%0Aelse%0A%7B%0AWrite-Error%20-Message%20%22%24(%24_.Exception)%22%20-ErrorAction%20Continue%0A%7D%0Athrow%20%22%24(%24_.Exception)%22%0A%7D%0Afinally%0A%7B%0AWrite-Output%20%22end%20job%22%0A%7D%0A%0A%0A%3C%2FMY_RESOURCE_GROUP%3E%3C%2FMY_ALERT_NAME%3E%3C%2FMY_RESOURCE_ID%3E%3C%2FVM_NAME%3E%3C%2FRESOURCE_GROUP%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Eboth%20of%20the%20method%20which%20I%20use%20returning%20an%20error%20that%20said%20my%20alert%20rules%20are%20not%20found.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-789785%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%2FEnable%20Azure%20monitor%20alert%20rules%20by%20azure%20automation%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-789785%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F346154%22%20target%3D%22_blank%22%3E%40orobmontana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHave%20you%20looked%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Fazure%2Fazure-monitor%2Fplatform%2Falerts-action-rules%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Fazure%2Fazure-monitor%2Fplatform%2Falerts-action-rules%3C%2FA%3E%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-816350%22%20slang%3D%22en-US%22%3ERe%3A%20Disable%2FEnable%20Azure%20monitor%20alert%20rules%20by%20azure%20automation%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-816350%22%20slang%3D%22en-US%22%3E%3CP%3Ehi%2C%20solved%20this%20problem.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ecreated%20thread%20for%20this%20problem%20also%20on%20Stack%20Overflow%2C%20please%20feel%20free%20to%20see%20the%20answer%20there%20%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F57372231%2Fdisable-enable-azure-monitor-alert-rules-by-azure-automation-powershell%2F57601335%2357601335%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F57372231%2Fdisable-enable-azure-monitor-alert-rules-by-azure-automation-powershell%2F57601335%2357601335%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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 (Occasional 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...