Automating VMware ESX snapshot notification

Discovering that your ESX hosts have been unknowingly creating open snapshots can be an alarming, not to mention dangerous, event.

Third party storage and backup vendors frequently call the vCenter API to issue a snapshot creation of running virtual machines before they grab a copy of the virtual machine’s hard disk – VMDK files – which are located on the VMware VMFS datastore. This process is typically followed by an immediate deletion of the snapshot file which will merge the changes back into the initial VMDK file. Problems can occur when this process does not complete successfully leaving you with an “open” snapshot. This problem is compounded when it occurs multiple times against the same guest.

Each open snapshot can significantly degrade virtual machine performance and also contribute to poor storage utilization. Unless you monitor each of your virtual machines on a daily basis, you could quickly be left with an uncomfortable situation on your hands.

The script below can be easily customized for your environment and provide regular email reports for open snapshots.

#Initialize VI Toolkit

Add-PSSnapin VMware.VimAutomation.Core

 

#Configure VC credentials

$Username = ‘service_account_name’

$password = ‘PASSWORD’

$server = ‘vcenter_host.domain.local’

 

#Setup email client

$SmtpClient = New-Object system.net.mail.smtpClient

$MailMessage = New-Object system.net.mail.mailmessage

$SmtpClient.host = “smtp_host.domain.local”

$MailMessage.from = “virtualcenter@domain.com”

$MailMessage.To.add(“admin@domain.com”)

$MailMessage.IsBodyHtml = 1

$MailMessage.Subject = “List of current VMware Snapshots”

 

#Get snapshots from all servers

Connect-VIServer $server -User $Username -Password $password

$Snaps = Get-VM | Get-Snapshot | Select VM,Name,Created

 

# Send email

$MailMessage.body = $Snaps | ConvertTo-Html

$SmtpClient.Send($MailMessage)

To execute the script, you will need to install Powershell and the VI Toolkit. Good luck!