gpt4 book ai didi

azure - 通过 azure Automation powershell 禁用/启用 Azure 监视器警报规则

转载 作者:行者123 更新时间:2023-12-02 06:19:18 31 4
gpt4 key购买 nike

我有一个 azure 的监控警报规则,它将每 5 分钟检查一次虚拟机的心跳。但是,虚拟机本身会在每天晚上 11 点下线,并在第二天上午 9 点再次启动。

所以我尝试使用 azure 自动化来同时禁用/启用我的警报规则。这是我尝试使用的代码:

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"
}

我使用的两种方法都会返回一个错误,表示找不到我的警报规则。

最佳答案

我同意 Joy 对新指标警报的回答,但是如果您的警报是“日志搜索”信号类型而不是“指标”信号类型,那么您必须禁用该警报,如下所示。

Update-AzScheduledQueryRule -ResourceGroupName "<resource group name>"  -Name "<alert name>" -Enabled 0

插图: enter image description here

相关引用:

  1. Update-AzScheduledQueryRule
  2. Add-AzMetricAlertRuleV2
  3. Get-AzMetricAlertRuleV2
  4. Get-AzScheduledQueryRule

希望这有帮助!!

关于azure - 通过 azure Automation powershell 禁用/启用 Azure 监视器警报规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57372231/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com