Archive for the ‘Alarms’ Tag

PowerCLI Script to automatically setup vCenter Alarm Email Notification.

After manually setting up email Alarm notifications for numerous vSphere deployments I decided it was time to harness PowerCLI and automate the process.

I found numerous examples of people who had already done this before me, so I figured it would be pretty easy.  It looks like the guys at built a script that several others adapted. VirtuallyMikeBrown had created a script for vSphere 4.1, but it wasn’t updated for vSphere 5 and only had a single email address.  Justin at Justin’s IT Blog had created a script that allowed three email addresses, but it couldn’t handle 4 or 2 or just 1 without editing the script or using dummy addresses.   And both of these scripts used a variable for each vCenter Alarm which meant if you added or removed an alarm you had to make multiple edits throughout the script.

So I decided I wanted my script to do the following:

  • Use arrays for both the alarms and email addresses to make it easier to add and remove entries.
  • Use a foreach loop to work through the alarms.
  • Have different classification or priorities for alarms so that some would notify repeatedly and with different frequencies. For example. You might not care if a VM is maxing out it’s CPU, but you definitely want to know if a host is.

These additional requirements took “easy” and turned it into a frustrating half-day, but I feel pretty good about the result.

Some notes about the script:

  • The $user, $pass and $vCenterServer variables need to be edited to be correct for your environment.
  • This currently sets up email alerts for almost every single default alarm in vCenter (based on 5.0).  This is undoubtedly overkill and you’ll likely want to remove some from the lists.
  • In the current setup this script takes a while to run through all the alarms.  It took about 10 minutes for me.  But that’s still a lot faster than doing it manually.
  • If you add or remove an alarm from the list make sure the following syntax is maintained:
    • Alarm names should be in double-quotes (“)
    • Alarms should be separated by commas (,)
    • Back-ticks (`) are used to continue a breakup the single line of alarm names into multiple lines for readability.   All alarms should have a back-tick at the end of the line except the last in the array.
  • If you want to add or remove email addresses make sure they too are in double-quotes, separated by commas with no spaces.
  • If you want to change the frequency of the repeating alerts just change the second number in the  “-ActionRepeatMinutes (60 * 24)” section.

This script is provided with no guarantees or warranties.  Use at your own risk.  Comments and feedback are welcome.

*********Updated 10/29/2012*******************

VMware decided to rename some alarms in 5.1 so I’ve now have two versions of the script.

vSphere 5.0 Script

vSphere 5.1 Script

Update 3/18/2015

Today I learned two things to be aware of:

  1. If the password contains special characters (see below) then those characters need to be escaped by prefixing the back tick or grave accent character (`).  So the password P@$$word would need to be entered as “P@`$`$word”
    1. Special characters in PowerShell are $ (  ) * + . [  ] ? \ / ^ { } |
  2. Since the vCenter Server Appliance uses Sendmail the delimiter between multiple email addresses has been changed from a semicolon to a comma. Because the PowerCLI command is the one formatting the entry with the semi-colon I’m not sure yet how to fix this.  (Thanks to AdminAfterWork for pointing this out)