Clear vSphere Alarms with PowerCLI

Sometimes there is a need to bulk clear alarms in vSphere. This can be done using the vSphere client and selecting some or all of the alarms, but what if you want to do this in code, perhaps as part of some wider automation. I was faced with a scenario where we had some planned maintenance to our Syslog servers, this work would trigger the Remote Syslog Connection Error alarm in vCenter, but how could I clear those alarms in code?

PowerCLI offers the functionality to acknowledge alarms. There’s no specific cmdlet, but we can retrieve the AlarmManager object with PowerCLI’s Get-View:

1$alarmManager=Get-View AlarmManager

This object represents the Alarm Manager in vCenter and Alarms can then be acknowledged using the AcknowledgeAlarm method, and it’s also possible to pick a specific alarm or type of alarm to acknowledge.

1$alarmManager.AcknowledgeAlarm()

We can also bulk clear alarms- for example “all the Red alarms”, or “all the host alarms” using the ClearTriggeredAlarms method of the AlarmManager object. The Clear-VSphereAlarm function in this PowerShell Gallery module is a good example of how to do that.

If we want to clear an individual alarm, or all alarms of a certain type (for example my Syslog alarms), this method won’t work. There is a workaround however, we can disable and then re-enable the Alarm. The following code snippet does just that, it finds all the triggered Remote Syslog Connection Error alarms and disables then enables them.

First we need to gather the objects

1#Get the Id of the Alarm Definition for the remote syslog being disconnected
2$AlarmId=(Get-AlarmDefinition -Name "Remote Syslog Connection Error").Id
3#Get the AlarmManager object and the Root Datacenter Folder.
4$alarmManager = Get-View AlarmManager
5$rootFolder = Get-Folder -Name Datacenters

And then we can process each Alarm in turn

1#Disable and then Enable each Remote Syslog Alarm.
2$rootFolder.ExtensionData.TriggeredAlarmState | `
3    Where-Object Alarm -eq $AlarmId | `
4    ForEach-Object -Process {
5            $alarmManager.DisableAlarm( $_.Alarm,$_.Entity)
6            $alarmManager.EnableAlarm( $_.Alarm,$_.Entity)
7        }