PowerCLI automation: Create vCenter Notification Email Alarm Action

I’ve received a request from a customer recently who wanted to configure all vCenter alarms to send a notification email. This instantly made me decide that PowerCLI was the best way to go rather than wasting a lot of valuable time in the vSphere client configuring the email notification alarms.

PowerCLI Automation Script:

I’ve created a quick and dirty script to do the job that I wanted to share with you. It’s probably not the fastest or the best optimized script, but it got the job done.

Get-AlarmDefinition | %{
   $_ | Set-AlarmDefinition -ActionRepeatMinutes (60 * 24);
   $_ | New-AlarmAction -Email -To "vcenteralarms@customer.corp" | %{
      $_ | New-AlarmActionTrigger -StartStatus "Green" -EndStatus "Yellow" -Repeat
      $_ | Get-AlarmActionTrigger | ?{$_.repeat -eq $false} | Remove-AlarmActionTrigger -Confirm:$false
      $_ | New-AlarmActionTrigger -StartStatus "Yellow" -EndStatus "Red" -Repeat
   }
}

Line 1: The script first retrieves all the alarms. You can easily adapt the script to update only a specific alarms by changing this line to include the name of the alarm you want to update like:

Get-AlarmDefinition "Datastore usage on disk"

or even all datastore alarms with:

Get-AlarmDefinition "Datastore*"

Line 2: The script then configures the alarm action repeat frequency to repeat the action every 24 hours. The value must be specified in minutes and the default value is to repeat every 5 minutes.

Line 3: On this line a new send notification email action is created to send an email to the email address vcenteralarms@customer.corp.

Line 4: This creates an alarm action trigger when the status changes from “Green” to “Yellow” or from “Normal” to “Warning”.

Line 5: During the creation of the send notification email alarm action, a default alarm action trigger is created. Because there’s currently no Set-AlarmActionTrigger cmdlet available, I decided that it’s easier to remove the default trigger rather than trying to change it from “Once” to “Repeat” by falling back to using the SDK methods. I warned you that it was quick and dirty ;-)

Line 6: finally this line creates an alarm action trigger when the status changes from “Yellow” to “Red” or from “Warning” to “Alert”.

Have Fun!

Related posts:

  1. A new PowerCLI release and a New Year’s feature Tweet On Decembre 2nd, VMware released PowerCLI 4.1.1 (or PowerCLI 4.1 Update1 as VMware calls it). Although this is a minor release, it contains more than just some bugfixes. The...
  2. PowerCLI: Disable/Enable HA and DRS Tweet Before upgrading my Virtual Center 2.5 server to vCenter Server 4.0, I decided to temporarily disable HA and DRS. This is just a precaution taken to avoid waiting for...
  3. PowerCLI: Reset CPU and Memory Limits Tweet Today I noticed a memory limit on a vm. After investigating my environment using the vEcoShell and the Community PowerPack, I found more vms with memory limits set. It turned out...
  4. PowerCLI 4.1 namespace changes Tweet Output type changes in PowerCLI 4.1In PowerCLI 4.1 VMware changed the namespaces of the output types. According to VMware, this was done to improve the internal structure and enable...
  5. Managing VMware DRS rules using PowerCLI Tweet One of the core features of VMware vSphere is the Distributed Resource Scheduler (DRS). VMware DRS is vSphere’s workload load balancer and relies on VMware vMotion technology to live-migrate...

9 Comments on “PowerCLI automation: Create vCenter Notification Email Alarm Action”

  1. #1 karlochacon
    on Feb 8th, 2012 at 3:04 am

    exactly What I was looking for

    few days ago I configured all alarms manually including the email was pretty tired

    I found this one too

    http://virtuallymikebrown.com/2011/11/03/add-e-mail-alerts-to-all-40-default-vsphere-4-1-alarms/

    my question Arnim is it works for vCenter 4.1 and 5? or only vCenter 5?

    thanks

  2. #2 Setting vCenter Alarms with PowerCLI | Sean Crookston
    on Feb 13th, 2012 at 2:02 pm
  3. #3 Arnim van Lieshout
    on Feb 14th, 2012 at 1:22 pm

    I’ve tested it on vCenter 5 and vCenter 4.1.

  4. #4 karlochacon
    on Feb 14th, 2012 at 3:33 pm

    thanks a lot good to know

  5. #5 Kevin
    on Feb 28th, 2012 at 11:47 pm

    Do you have one that removes the typo’d email address I put in? ;-)

    Thank you for this!

  6. #6 JC
    on Jul 9th, 2012 at 3:55 pm

    I have tried running this command against our vCenter 5.0.0 Build 623373 but it errors out stating that Alarm name already exists. Has anyone else experienced this and know a way around it? This script works great if I go in and rename the original name or create a custom alarm.

  7. #7 JC
    on Jul 9th, 2012 at 8:57 pm

    Nevermind, requires the VMware-PowerCLI to be upgraded to version 5, then it works properly.

  8. #8 TheCleaner
    on Nov 16th, 2012 at 5:39 pm

    Just wanted to say thanks for the script. Works perfect, nice and simple.

  9. #9 Dan Daffer
    on Nov 26th, 2012 at 7:56 pm

    Insert this on line #2 to be able to remove and re-apply settings:

    $_ | Get-AlarmAction | ?{$_.to -eq “your_email@your_domain.com”} | Remove-AlarmAction -Confirm:$false

Leave a Comment