Using PowerCLI to set mail recipient for a bulk of alarms

Using PowerCLI to set mail recipient for a bulk of alarms

In this post I will describe how to use PowerCLI to add a trigger and a mail recipient to selected vSphere alarms. This is an excellent example of how PowerCLI can easily be used to do a task in a few moments while needing maybe hours when doing in GUI.

A use-case for assigning a trigger and recipient to many alerts can be the implementation of a software that adds alerts to vSphere. I will use the installation of HPE SimpliVity to demonstrate my commands. After installation of SimpliVity vCenter Plug-In there are about 180 new alarms in vSphere.

Configure mail-settings in vCenter

Sending mails when alarms are triggered requires a mail-configuration in vCenter. Because we want to use PowerCLI to configure alarms, we will also use it to set these vCenter-settings. For this purpose, use a variable when connecting to vCenter.

$vCenter = Connect-VIServer vCenterFQDN/IP

To see mail/SMTP specific settings in vCenter, run:

$vCenter | Get-AdvancedSetting |  Where-Object {$_.name -like "mail*"}

Important settings are: SMTP server, port and sender. To set these values run these commands:

$vCenter | Get-AdvancedSetting –Name mail.smtp.server | Set-AdvancedSetting –Value smtp.example.com -Confirm:$false
$vCenter | Get-AdvancedSetting –Name mail.smtp.port | Set-AdvancedSetting –Value 25 -Confirm:$false
$vCenter | Get-AdvancedSetting –Name mail.sender | Set-AdvancedSetting –Value sender@example.com -Confirm:$false

Note: Use -Confirm:$false to suppress confirmation for each setting.
Hint: If you missed using a variable at connecting to vCenter, use variable $global:DefaultVIServer instead.

Configure alarms to send mails

To get defined alarms to send mails when they are triggered, you need to set an mail recipient when the color of the alarm changes. You can set actions – as sending a mail – when color changes to yellow, red or green. The changing of the color is the trigger to fire the action. So, an alarm that sends a mail consists of these components:

  • Alarm definition
    Definition describes under which circumstances a problem in form of an alarm gets issued. For example: Free space of a datastore drops beneath 10 %.
  • Alarm trigger
    Description of alarm color change. For example: green to yellow or/and yellow to red.
  • Alarm action
    Defines what to do when alarm gets triggered. Examples: send mail, run script.

In my use-case I want to configure all SimpliVity-alarms to send mails, when color is changing in direction to red. This means I want to get a mail, when alarm changes from green to yellow and from yellow to red. This includes a change from green to red directly, because yellow is also set in between for a moment. When you try to set a trigger that skips a color-code, you will get an error.

To list all SimpliVity alarms, run:

Get-AlarmDefinition -Name simplivity*

Note: Of course you can use another filter, to select alarms you want to configure! For example use *vSAN* instead of simplivity* to configure vSAN-alarms!

To set alarm action to send a mail for each SimpliVity-alarm run:

Get-AlarmDefinition -Name simplivity* | New-AlarmAction -Email -To recipient@example.com

Note: After running this command, alarm trigger: yellow to red is automatically set. This is because no alarm action can be set without at least one alarm trigger.

To see set alarm trigger, run:

Get-AlarmDefinition -Name simplivity* | Get-AlarmAction | Get-AlarmActionTrigger

To remove send mail action again, run:

Get-AlarmDefinition -Name simplivity* | Get-AlarmAction | Remove-AlarmAction -Confirm:$false

Up to now a mail gets send when alarm reaches red status. To add mail-action when alarm gets status yellow, run:

Get-AlarmDefinition -Name simplivity* | Get-AlarmAction | New-AlarmActionTrigger -StartStatus Green -EndStatus Yellow

Note: When no action is set, this command does nothing.

2 responses to “Using PowerCLI to set mail recipient for a bulk of alarms”

  1. Alex says:

    Hello.
    If you have more than one alarm action, i recommend use next commands for add action when alarm gets status yellow:
    Get-AlarmDefinition -Name simplivity* | Get-AlarmAction | where {$_.ActionType -like ‘SendEmail’} | New-AlarmActionTrigger -StartStatus Green -EndStatus Yellow

  2. woifgaung says:

    Hello Alex! Thanks for your command!

Leave a Reply

Your email address will not be published. Required fields are marked *