Search code examples
vmwarepowpowercli

How can I configure vCenter Alarm by Cluster with PowerCLI


I have a code to configure the vcenter alarm, however, I would like to create a alarm in per Cluster level.

Please advise me any direction I can go to.

Here is the code, but it won't work

$MinutesToRepeat = "10"
$alarms = @("Testing")

$cluster = Get-Cluster "Prod Cluster"
foreach ($alarm in $alarms) {
    Set-AlarmDefinition -Name $alarm | %{
        $_ | Set-AlarmDefinition -ActionRepeatMinutes $MinutesToRepeat;  
        $_ | Get-AlarmAction -ActionType "SendEmail" | Remove-AlarmAction -Confirm:$false 
        $_ | New-AlarmAction -Email -To $AdminEmail | %{
            $_ | New-AlarmActionTrigger -StartStatus "Green" -EndStatus "Yellow" 
            $_ | New-AlarmActionTrigger -StartStatus "Yellow" -EndStatus "Red" -Repeat
            $_ | New-AlarmActionTrigger -StartStatus 'Red' -EndStatus 'Yellow'
            $_ | New-AlarmActionTrigger -StartStatus 'Yellow' -EndStatus "Green"
        }
    }
}

Solution

  • Try changing to the below code, basically by default when creating a trigger yellow to red is set to "Once" this needs to be removed before being added back in.

    foreach ($alarm in $alarms) {
        Set-AlarmDefinition -Name $alarm -AlarmDefinition $alarm -ActionRepeatMinutes $MinutesToRepeat | %{
            $_ | Get-AlarmAction -ActionType "SendEmail" | Remove-AlarmAction -Confirm:$false 
            $_ | New-AlarmAction -Email -To $AdminEmail | %{
                $_ | New-AlarmActionTrigger -StartStatus Green -EndStatus Yellow 
                $_ | New-AlarmActionTrigger -StartStatus Red -EndStatus Yellow
                $_ | New-AlarmActionTrigger -StartStatus Yellow -EndStatus Green
            }
    
        }
        $AlarmAction = Get-Alarmdefinition -Name $alarm | Get-AlarmAction -ActionType 'SendEmail'
        $AlarmAction.Trigger | Where {($_.StartStatus -eq 'Yellow') -And ($_.EndStatus -eq 'Red')} | Remove-AlarmActionTrigger -Confirm:$False
        $AlarmAction | New-AlarmActionTrigger -StartStatus Yellow -EndStatus Red -Repeat
    }