Pages

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 = "vcenter.abc.com"
$user = "abc\admin-vc"

$pass = "ABCPassword"
$SMTPserver = "smtp.
abc.com"
$from = "vcenter@
abc.com"
$to = "alerts@
abc.com"# -------------------------------------------------------
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 = "vcenter.yourdomain.org"
$user = "Windows-UPN\admin-user"
$pass = "Password"

$SMTPserver = "smtp.yourdomain.org"
$from = "vcenter@yourdomain.org"
$to = "alerts@yourdomain.org"
# ------------------------------------------------------------

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 {
color:#3366FF;
font-size:12.0pt;
font-weight:700;
font-family:Verdana, sans-serif;
text-align:left;
vertical-align:middle;
height:30.0pt;
width:416pt
}
.colnames {
color:white;
font-size:8.0pt;
font-weight:700;
font-family:Tahoma, sans-serif;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#99CC00;
}
.text {
color:windowtext;
font-size:8.0pt;
font-family:Arial;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#CCCCFF;
}
</style>
<table border=0 cellpadding=0 cellspacing=0 width=555
style='border-collapse:collapse;table-layout:fixed;width:600pt'>
<tr style='height:15.0pt'>
<th colspan=5 height=40 width=555 class="heading">
vSphere Snapshots Daily Report</th>
</tr>
<tr>
<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>
</tr>
"@

$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
$mailer.send($msg)

13 comments:

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

    ReplyDelete
  2. 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

    ReplyDelete
  3. THANKS, that was what I was looking for :)

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

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

    ReplyDelete
  6. Fantastic script, thank you!

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

    ReplyDelete
  8. 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?

    Example:

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

    ReplyDelete
  9. 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

    ReplyDelete
  10. 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. http://www.logbookloans4uk.co.uk

    ReplyDelete
  11. Is there a way to add a column to see the "day age" of a snapshot?

    ReplyDelete
  12. Any know how you can loop this for multiple vcenter servers and combine into single email report?

    ReplyDelete

Popular Posts