Home > VMware > VMware: Change Alarm-Actions with PowerCLI

VMware: Change Alarm-Actions with PowerCLI

I’m working on a project for a company that is running a big cloud datacenter, the organization is 24/7 and some of my colleagues need to monitor the environment week by week (standby shifts). Once a week we need to change some alarm actions to send the Alarm notifications to a different email address.. because it’s a lot of work to do this by hand I tried to script this.

You must change the email address value in the script.. and automatically change the rest of the definitions.

$MailToRandom1 = ""

Here’s my script:

#   SCRIPT:        Change_Alarm_Datacenter.ps1                       #
#   FUNCTION:      Modify multiple Alarm definitions in vCenter      #
#   CREATED:       10/06/2011                                        #
#   VERSION:       v.1.1                                             #
#   CHANGELOG:                                                       #
#                                                                    #
#    v.1.1                                                           #
#    - Deleted email body                                            #
#    - Added Alarm definition                                        #
#    - Added changelog                                               #
#                                                                    #
#    v.1.0                                                           #
#    - First Release                                                 #
#                                                                    #
# Note: You only have to change the "MailToRandom1" value in "Custom #
#         Definitions" to change the alarm action                    #
# Connect vCenter server;
$vCenterServer = "vcenter-server"
$user = "username"
$pass = "password"

if ( $DefaultVIServers.Length -lt 1 )
  Connect-VIServer -Server $vCenterServer -Protocol https -User $user -Password $pass -WarningAction SilentlyContinue | Out-Null

# Custom Definitions;
$actType = "SendEmailAction"
$MailToRandom1 = ""
$MailToDefault1 = ""
$MailToDefault2 = ""
$actAlarm1 = "Machine suspended"
$actAlarm2 = "Datastore usage"
$actAlarm3 = "Snapshot alarm"
$actSubject = "Get-AlarmDefinition"
# Delete Alarm Trigger;
Get-AlarmDefinition -Name "$actAlarm1" , "$actAlarm2" , "$actAlarm3" | Get-AlarmAction | Remove-AlarmAction -Confirm:$false
# Create Alarm Trigger;
Get-AlarmDefinition -Name "$actAlarm1" , "$actAlarm2" , "$actAlarm3" | New-AlarmAction -Email -To "$MailToRandom1" , "$MailToDefault1" , "$MailToDefault2"
Get-AlarmDefinition -Name "$actAlarm1" , "$actAlarm2" , "$actAlarm3" | Get-AlarmAction | New-AlarmActionTrigger -StartStatus "Green" -EndStatus "Yellow"
# Disconnect vCenter server;
Disconnect-VIServer -Server $vCenterServer -Force:$true -Confirm:$false


Sander Daems is founder and author of this blog and working as a Lead (Sr.) Consultant by UNICA ICT Solutions. Sander has over 15 years experience in IT, primary focus: virtualization and modern worksplace.

More Posts - Website

Follow Me:

  1. Chris Robinson
    October 25th, 2011  (Quote) at 21:28  (Quote) | #1

    I think this may be exactly what I am looking for, but I have a few questions. Forgive me I am not a programmer, so these questions may be ignorant.

    1) Will this change all alarm definitions to the email address you specifiy in $MailToRandom1? Currently I have all the default alarms vmware provides but none of them are setup to alert. I want to change all the alarms to alert to the same email address.

    2) Is this the only way you know how to change multiple alerts at once?

    Thanks for your help!!

  2. October 26th, 2011  (Quote) at 09:17  (Quote) | #2

    Hi Chris

    I created this script for standby users so sometimes I need to change 1 email address a alarm, this script delete all the value’s in the alarm re-create them.

    $MailToRandom1 = random email address, I change this value every standby shift
    $MailToDefault1 = static (email address) value
    $MailToDefault2 = static (email address) value

    So $MailToDefault1 is standard going to, $MailToDefault2 is going to (this setting I never wanna change)

    If you want to email/alert a diffrent person you can change the $MailToRandom1

    This is a dirty way to script.. you delete all actions in the alarm and re-create it..

    Answer 2: yes, this is the only way

  3. Chris Robinson
    October 26th, 2011  (Quote) at 14:25  (Quote) | #3

    Great! I only need to set them one time, but I didn’t want to have to go into each alarm and set the action. So I think it would work for me.

    Thanks for your help!

  4. July 9th, 2012  (Quote) at 18:02  (Quote) | #4

    Great script. Found it on a couple other sites to, but wanted to make sure to thank the original author. I used parts of this script to create my own using arrays for the email addresses and alarms. I also built it to allow for repeating notifications. Hopefully someone else gets some use out of it too.

  5. May 28th, 2014  (Quote) at 22:32  (Quote) | #5

    Wonderful post however , I was wanting to know if you could write a litte more on this subject?
    I’d be very thankful if you could elaborate a little bit further.
    Appreciate it!

  1. at | #1
  2. at | #2
  3. at | #3