Check For HA Events with PowerCLI

posted by Mike Nisk on Mar 29, 2016


You can check for vSphere HA events in a number of ways. I find that using PowerCLI is the best.

How To

Just hit enter to accept the defaults. Optionally, you can enter the number of hours and events to search on.

# CheckForHAEvents.ps1
# Quick one-liner to check for recent HA events.
# Note:  Adjust "MaxSamples" to a higher number to search further back
# Based on:
# http://www.jonathanmedd.net/2012/03/which-vms-restarted-after-a-vsphere-ha-event.html

#More Info:

Write-Host "`nWelcome to vSphere HA Checker`n" -ForegroundColor Yellow
Write-Host "This script:"
Write-Host "- Checks for any recent HA Events"
Write-Host "- If any VMs were hard rebooted by HA, they will show up here"
Write-Host "- User provides the time range (how far back in the logs to search)"
Write-Host "- User provides max number of VI events to review`n"

$range = Read-Host "Enter the time range in hours (default 24 hours)"
If(!($range)){$range = 24}

$MaxSamples = Read-Host "Enter the max number of events to search (default 100000)"
If(!($MaxSamples)){$MaxSamples = 100000}

Write-Host "`nParsing vSphere logs..." -ForegroundColor Yellow
$Results = Get-VIEvent -start (Get-Date).addhours(-$range) -MaxSamples $MaxSamples | Where-Object {$_.eventtypeid -eq "com.vmware.vc.ha.VmRestartedByHAEvent"} | Select ObjectName,CreatedTime

## Echo results to screen
    Write-Host "No HA Events found in the past $range hour(s)`n" -ForegroundColor Green
Else {
    Write-Host "HA Events Detected on:"

Note: If any VMs have been hard rebooted by vSphere HA, they will be listed.

Category:PowerCLI Tags: vSphere HA Availability PowerCLI