VMware: Change Alarm-Actions with PowerCLI

8

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 = "[email protected]"

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 = "[email protected]"
$MailToDefault1 = "[email protected]"
$MailToDefault2 = "[email protected]"
$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
#--------------------------------------------------------------------#

About Author

8 thoughts on “VMware: Change Alarm-Actions with PowerCLI

  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. 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 [email protected], $MailToDefault2 is going to [email protected] (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. 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.
    http://fixingitpro.com/2012/07/09/powercli-script-to-automatically-setup-vcenter-alarm-email-notification/

Leave a Reply

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