Post

Bulk configure vCenter Alarms with PowerCLI

1452988800
1614629284
6

I was recently asked if it was possible to update vCenter alarms in bulk with email details. So i set about writing the below script, basically this script will go through looking for any alarms that match the name you specify and set the email as required.

This is a really basic script and can easily be modified to set alarms how you want them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$MinutesToRepeat = "10"
$alarms = @("Testing Alarm")
$cluster = Get-Cluster "Test Cluster"
$AdminEmail = "your@email.com"
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
}

To edit multiple alarms at once simply change the $alarms variable as below:

1
$alarms = @("Test Alarm1", "Test Alarm2")

One thing you will probably notice is that we set the “Yellow” to “Red” status after everything else, the reason for this is that it is set by default when creating the alarm definition and we need to unset this before resetting with the required notification type.

This post is licensed under CC BY 4.0 by the author.