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)
4 comments:
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 :-)
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
THANKS, that was what I was looking for :)
Great script, anyone know how to take it a step further and log who created the snapshot?
Post a Comment