VM restarted due to host failure


Are you looking for the VMs restarted due to Host failure?

Imagine your ESXi host suddenly has a big problem, and you need to know which virtual machines were affected. This information is crucial, and many people, like Server administrators, really want to know it. I’m excited to share how we can find this out.

Below is the code that will help you to find the affected VMs.

Get-VIEvent -MaxSamples 100000 -Start (Get-Date).AddDays(-1) -Type Warning | Where {$_.FullFormattedMessage -match “restarted”} |select CreatedTime,FullFormattedMessage | sort CreatedTime –Descending

With just one line of PowerShell, we fetched warning events from the last 24 hours, searched for the word “restarted” in them, and neatly displayed them in a list. With this list, we could easily begin checking the VMs for issues.

Now if you are too lazy to make use of your mind and looking for a complete code that sends you the list of VMs on email then here you go.

This script will help you to find the affected VMs.

# Define variables
$VMHost = “10.0.xx.xx” # Replace with the name of your VM host
$EmailServer = “smtp.abc.net” # Replace with your SMTP server

$SenderEmail = “HAreport@abc.com” # Replace with your email address
$RecipientEmail = “as@abc.com” # Replace with the recipient’s email addresses

# Connect to your virtualization environment (e.g., VMware vSphere)
Connect-VIServer -Server $VMHost
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

# Get a list of VMs that were restarted due to HA events
$RestartedVMs = Get-VIEvent -MaxSamples 100000 -Start (Get-Date).AddDays(-1) -Type Warning | Where-Object { $_.FullFormattedMessage -match “restarted” } | Select-Object CreatedTime, FullFormattedMessage | Sort-Object CreatedTime -Descending

# Generate the report
$Report = “Virtual Machines Restarted due to HA Events:`r`n`r`n”
if ($RestartedVMs.Count -gt 0) {
$RestartedVMs | ForEach-Object {
$Report += “VM Name: $($_.Name)`r`n”
} else {
$Report = “No VMs were restarted due to HA events in the last 24 hours.”

# Disconnect from the virtualization environment
Disconnect-VIServer -Server $VMHost -Confirm:$false

# Send the report via email
Send-MailMessage -SmtpServer $EmailServer -From $SenderEmail -To $RecipientEmail -Subject “HA Event Report ” -Body $Report

# Display a confirmation message
Write-Host “HA Event report sent to $RecipientEmail”

Below is the explanation of the above script 

  1. We start by defining your vCenter Server, authentication credentials, and email parameters.

  2. The script connects to the vCenter Server using the provided credentials.

  3. It retrieves a list of VMs that were restarted due to HA events in the last 24 hours.

  4. The script generates a report listing the restarted VMs.

  5. It disconnects from the vCenter Server after collecting the required information.

  6. Finally, it sends an email report with the list of restarted VMs to the team and displays a confirmation message.

Remember to replace the placeholders in the script with your actual server, credentials, and email details. This script will help you automatically identify and report VMs restarted due to HA events.

In conclusion, we’ve explored how to use PowerShell and VMware PowerCLI to automate the process of identifying virtual machines that have been rebooted due to High Availability (HA) events and sending a comprehensive report to your team. By implementing this script, you can save valuable time and ensure the smooth operation of your virtualized infrastructure. We hope that the insights and script provided in this article prove to be a useful resource for your IT tasks. If you have any questions or need further assistance, please feel free to reach out. Wishing you success in managing your virtual environment efficiently and proactively.

VMware Mastery

Hi, I've been working with Windows, VMware and Cloud technologies for more than 12 years. I love delving into the exciting realm of technology and enjoy sharing my knowledge with others. I write about IT and Technology, covering both technical and non-technical topics.

Leave a Reply