Popular Posts

Thursday, March 11, 2010

PowerCLI Script for Snapshot Reporting (vSphere)

VMware vSphere or VI3 doesn't provide a native way to run scheduled snapshot reports. However, having a daily snapshot report can be very useful specially if you are using an snapshot based backup software to backup your VMs like Vizioncore's vRanger Pro. Orphaned or uncleaned snapshots can be problematic because they grow in size and can fill up space on the datastore where snapshot is residing. These lingering snapshots can cause VMs to become corrupt or other new backup jobs from completing.

For VI3 you can write a perl script that runs on each ESX host. However, now VMware provides a scripting mechanism based on PowerShell called PowerCLI which has over 140 cmdlets used by VMware admins to manage vSphere and VI3 (3.5 Update 2 and above) environment.

I couldn't find any script online that would provide this functionality so I wrote a PowerShell script using PowerCLI myself and here it is.

You can download the script from here: Generate VMware VM Snapshots Reports PowerCLI Script

Output looks like this:

Setup Instructions:

1. Download the zip file to local vCenter server and extract to a folder. There are three files .ps1, .bat and .xml.
2. Modify the script configuration section to include your company's information For example
# Configuration Parameters: Update your information here.
# -------------------------------------------------------
$server = ""
$user = "abc\admin-vc"

$pass = "ABCPassword"
$SMTPserver = "smtp."
$from = "vcenter@"
$to = "alerts@"# -------------------------------------------------------
3. Install PowerShell and PowerCLI on vCenter server
4. Enable remote script execution by running Set-ExecutionPolicy RemotelySigned
5. Schedule the script in Windows Scheduler using the batch file provided.
Full Script
# Created 3/5/2010
# Purpose: Creates and email snapshot report in html format for all VMs running on VMWare vSphere
# Usage Notice: Use at your own risk
# Configuration Parameters: Update your information here.
# -------------------------------------------------------------
$server = ""
$user = "Windows-UPN\admin-user"
$pass = "Password"

$SMTPserver = ""
$from = ""
$to = ""
# ------------------------------------------------------------

if ( $DefaultVIServers.Length -lt 1 )
Connect-VIServer -Server $server -Protocol https -User $user -Password $pass -WarningAction SilentlyContinue Out-Null

# Format html report
$htmlReport = @"
<style type='text/css'>
.heading {
font-family:Verdana, sans-serif;
.colnames {
font-family:Tahoma, sans-serif;
border:.5pt solid windowtext;
.text {
border:.5pt solid windowtext;
<table border=0 cellpadding=0 cellspacing=0 width=555
<tr style='height:15.0pt'>
<th colspan=5 height=40 width=555 class="heading">
vSphere Snapshots Daily Report</th>
<th class="colnames">Name</th>
<th class="colnames">Size (MB)</th>
<th class="colnames">VM</th>
<th class="colnames">VM State</th>
<th class="colnames">Date Created</th>

$vmlist = Get-VM -Server $server
$snapshotCount = 0

ForEach ($vm in $vmlist)
# List snaphosts
$snapshots = Get-Snapshot -VM (Get-VM -Name $vm.Name) -WarningAction SilentlyContinue

if ($snapshots -ne $null)

ForEach ($snapshot in $snapshots)
if( $snapshot -ne $null)
$snapshotCount = $snapshotCount + 1
$htmlReport = $htmlReport +
"<tr><td class='text'>" + $snapshot.Name + "</td>" +
"<td class='text'>" + $snapshot.SizeMB + "</td>" +
"<td class='text'>"+ $vm.Name + "</td>" +
"<td class='text'>" + $vm.PowerState + "</td>" +
"<td class='text'>" + $snapshot.Created + "</td></tr>"

$htmlReport = $htmlReport + "</table>"

Disconnect-VIServer -Server $server -Force:$true -Confirm:$false

# Send email
$subject = "vSphere Snapshots Daily Report: " + $snapshotCount + " snapshots"
$emailbody = "No snapshot was found."
if( $snapshotCount -gt 0 )
$emailbody = $htmlReport
$mailer = New-Object Net.Mail.SMTPclient($SMTPserver)
$msg = New-Object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
$msg.IsBodyHTML = $true


Frank said...

The download link for the Script is broken. It points to [www.], which has a broken DNS A-record of "". Sorry, unfortunately I can't reach your private network :-)

Anonymous said...

Thank you! Great script.

I would like to correct one error in your post. You say run the command:

Set-ExecutionPolicy RemotelySigned

It should actually be:

Set-ExecutionPolicy RemoteSigned

Anonymous said...

THANKS, that was what I was looking for :)

Anonymous said...

Great script, anyone know how to take it a step further and log who created the snapshot?

Tax Attorney said...

It is so nice that you wrote this whole script by yourself for us. You did help others with no cost i appreciate you.

Anonymous said...

Fantastic script, thank you!

Anonymous said...

That's cool. I don't have smtp server available. Do you know how to redirect the output to the file?

Anonymous said...

Thank you for your excellent post! Is there a way to show the number of days from the date the snapshot has been created instead of just the date?


Name Size (MB) VM VM State Days
Test patch 4113.27 TestGuest PoweredOn 2

Web development Company said...

This is a good post. This post give truly quality information.I’m definitely going to look into it.Really very useful tips are provided here.thank you so much.Keep up the good works

Mark Joy said...

It is therefore nice that you just wrote this whole script by yourself for North American country. you probably did facilitate others with no price i appreciate you.

laurent charfi said...

Many Thanks ! You Rocks dude !

Post a Comment