VMware Cloud Community
jessem
Enthusiast
Enthusiast

PowerCLI script for Alarms check

Hi All,

I am looking for a script to check about 5 vCenters to see what alarms are disabled whether it's on a host, a cluster or even a datacenter.  And that I would like that emailed to me with only those objects that are disabled.

Reply
0 Kudos
20 Replies
LucD
Leadership
Leadership

Try something like this

The script assumes there is a connection to all vCenters.

$toAddr = 'lucd@lucd.com'

$fromAddr = 'report@lucd.com'

$smtpServer = 'mail.lucd.com'

Send-MailMessage -To $toAddr -From $fromAddr -Subject 'Disabled Alarms' -SmtpServer $smtpServer -BodyAsHtml -Body (

    Get-AlarmDefinition |

    where{!$_.Enabled} |

    Select Name,@{N='Entity';E={$_.Entity.Name}},@{N='vCenter';E={$_.Uid.Split('@')[1].Split(':')[0]}} |

    ConvertTo-Html |

    Out-String

)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

LucD, I ran it and it did not pickup the alarm.  I disabled alarm actions on a host and it did not show that.  Any ideas?

Reply
0 Kudos
LucD
Leadership
Leadership

Does the following return any alarms, that are disabled ?

Get-AlarmDefinition -Enabled:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

Yes, but that alarm is not the one I am looking for.  We disable alarms on host objects.  Right-click host, disable alarm actions.  That's the one I am looking for to report on.

Reply
0 Kudos
LucD
Leadership
Leadership

That options disables all alarms for that host.

You can report on those ESXi server with this

$toAddr = 'lucd@lucd.com'

$fromAddr = 'report@lucd.com'

$smtpServer = 'mail.lucd.com'

Send-MailMessage -To $toAddr -From $fromAddr -Subject 'Disabled Alarms' -SmtpServer $smtpServer -BodyAsHtml -Body (

    Get-VMHost |

    where{!$_.ExtensionData.AlarmActionsEnabled} |

    Select Name |

    ConvertTo-Html |

    Out-String

)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

hey LucD that worked out great!  Now is there a way that I can have that check all 5 vcenters and email me like this below

Subject: Disabled Alarms

VCENTER 01

esxihost01

esxihost02

VCENTER02

esxihost99

esxihost100

VCENTER03

esxihost09

esxihost10

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

also, would there be a reason the html email comes in and has an asterisks at the beginning like seen here?

   *

esxihost01

esxihost02

Reply
0 Kudos
LucD
Leadership
Leadership

This version will include the vCenter on each line.

On the asterisk, which PowerShell version are you using ?

Display the contents of $PSVersionTable

$toAddr = 'lucd@lucd.com'

$fromAddr = 'report@lucd.com'

$smtpServer = 'mail.lucd.com'

Send-MailMessage -To $toAddr -From $fromAddr -Subject 'Disabled Alarms' -SmtpServer $smtpServer -BodyAsHtml -Body (

    Get-VMHost |

    where{!$_.ExtensionData.AlarmActionsEnabled} |

    Select @{N='vCenter';E={$_.Uid.Split('@')[1].Split(':')[0]}},Name |

    ConvertTo-Html |

    Out-String

)

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

LucD,

I am using version PowerCLI v6.2.9200.16481.

I tried the script and it works.  However, I guess I am still confused on where I specify the multiple vCenters.  When I open up PowerCLI and I do a connect-viserver, I am specifying just 1 vCenter and of course the script works for that one vCenter.  Is there a trick that I am missing on how to specify multiples?

Reply
0 Kudos
LucD
Leadership
Leadership

Try connecting to all vCenters before running the script.

You can the connections in $global:defaultviservers.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

like this?

$global:defaultviservers = 'vcenter01,vcenter02'

$toAddr = 'lucd@lucd.com'

$fromAddr = 'vmalarms@lucd.com'

$smtpServer = 'smtp.com'

Send-MailMessage -To $toAddr -From $fromAddr -Subject 'Disabled Alarms' -SmtpServer $smtpServer -BodyAsHtml -Body (

    Get-VMHost |

    where{$_.ExtensionData.AlarmActionsEnabled} |

    Select @{N='vCenter';E={$_.Uid.Split('@')[1].Split(':')[0]}},Name |

    ConvertTo-Html |

    Out-String

)

Reply
0 Kudos
LucD
Leadership
Leadership

No, on the Connect-VIServer cmdlet you can pass all 5 vCenters.

You can check afterwards what is connected by looking at the content of $global:defaultvirservers


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

Got it!  As you can see, quite a novice here.  So I ran the script and it worked but when I checked the output, a bunch of hosts that are listed in the email do not have the alarms disabled.  I went into vCenter and double-checked and sure enough a bunch of hosts the report listed as alarms disabled are not.  What do you think?

Reply
0 Kudos
LucD
Leadership
Leadership

No problem, we all had to start at some point Smiley Wink

What does it show when you execute the following for one of the ESXi nodes that is wrongfully listed in the email ?

Update the Name !

Get-VMHost -Name WrongHost |

Select Name,@{N='Alarm Enabled';E={$_.ExtensionData.AlarmActionsEnabled}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

When I executed the below, it doesn't show anything....brings me back to a cursor....

Get-VM -Name myhostname |

>> Select Name,@{N='Alarm Enabled';E={$_.ExtensionData.AlarmActionsEnabled}}

>>

Reply
0 Kudos
LucD
Leadership
Leadership

Hit <enter> once  more


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

Ok, after I hit enter once more, it shows the name of the server host I specified and Alarms Enabled True.

Reply
0 Kudos
LucD
Leadership
Leadership

Now I'm lost.

The host that is included in the email shows that alarms actions are enabled, but then it shouldn't have passed the Where-clause.

Could you perhaps attach, as a file, the actual script your are using ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jessem
Enthusiast
Enthusiast

Sorry LucD, problems like always.  Here's the script I'm using.

$toAddr = 'me@me.com'

$fromAddr = 'me@me.com'

$smtpServer = 'smtp@me.com'

Send-MailMessage -To $toAddr -From $fromAddr -Subject 'Disabled Alarms' -SmtpServer $smtpServer -BodyAsHtml -Body (

    Get-VMHost |

    where{$_.ExtensionData.AlarmActionsEnabled} |

    Select @{N='vCenter';E={$_.Uid.Split('@')[1].Split(':')[0]}},Name |

    ConvertTo-Html |

    Out-String

)

Reply
0 Kudos