VMware Cloud Community
jessem
Enthusiast
Enthusiast

Script to Re-Enable Alarms in vCenter

Ok here is my deal.  Sometimes as engineers, we forget to re-enable host alarms.  When we are doing maintenance, we right-click host, disable alarms.

I want to be able to run an automated script maybe via Task Scheduler in Windows to re-enable all the host based alarms once a day nightly.  I want to be able to re-enable all the host alarms on All hosts within that vCenter.  However, in this example, I thought I was re-enabling alarms based on any host that starts with a D.  This script is not working.  Anyone have suggestions on the wildcare use for ALL HOSTS and then the option for specified hosts that start with just the D letter?

$alarmMgr = Get-View AlarmManager

$esx = Get-VMHost D*

$alarmMgr.EnableAlarmActions($esx.Extensiondata.MoRef,$true)

0 Kudos
3 Replies
ccalvetTCC
Enthusiast
Enthusiast

Could you please try:

For all hosts

$alarmMgr = Get-View AlarmManager

Get-VMHost | foreach-object{

$alarmMgr.EnableAlarmActions($_.Extensiondata.MoRef,$true)

}

For host starting with D.

$alarmMgr = Get-View AlarmManager

Get-VMHost D* | foreach-object{

$alarmMgr.EnableAlarmActions($_.Extensiondata.MoRef,$true)

}

(I am not able to test the script right now)

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
0 Kudos
jessem
Enthusiast
Enthusiast

That worked great!  Thanks.  Question for you.  Could I automate this script to run in Windows (task scheduler) every morning?

0 Kudos
ccalvetTCC
Enthusiast
Enthusiast

First you will need to obtain:

an account that will be used to execute this scheduled task (Ideally a service account for this purpose with just enough rights to do the job)

a host where will you run this scheduled task and the version of PowerCLI/PowerShell associated (For example a 2012R2 server. Avoid a personnal computer)

confirm that the host is in the same AD that is configured for the VC (It you are planning to use windows authentication to connect to the VC)

Then modify the script:

The information below will be a bare minimum assuming that the account that will execute the schedule task is able to connect via windows authentication to the VC

connect-viserver "your vCenter"

#All the script here

disconnect-viserver -Server "your vCenter" -Confirm:$False

And save it in a path reachable from the "PowerCLI Host" in a file "yourscriptname.ps1"

(For a script that will run every day adding error handling in the script will be a must)

Then you will have to create a schedule task in Windows to execute this "yourscriptname.ps1" under the account identified in the first step

2 solution at this stage:

Only execute the script
Identify all snapin/modules needed for your script and add them in the first lines of "yourscriptname.ps1"

(This is why knowing which version of PowerCLI is necessary. Since PowerCLI 6 some commands are available as module and not as snapin)

Load "PowerCLI" environment and execute the script. (Using psconsolefile and vim.psc1)

More details there:

Running a PowerCLI Scheduled task

Fixing a (minor) PowerCLI 6 R1 issue

Schedule task with PowerCLI script

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
0 Kudos