Posts Tagged PowerCli

Monitor VMware for SnapShots

Here is something I recently put together to monitor VMware Snapshots. 

The issue: The backup solution creates snapshots daily but if something goes wrong it doesn’t clean up a snap.

The solution: Using this simple powercli script, we monitor and check to see if any VM’s have snapshots over 1 day old. If so we highlight them in the report, other snapshots are simply in the report.

Side Note: Not sure if I’m happy with the colors I used in the report but it does grab ones attention.


#=====================================================================================================#
# Author: Robert Stacks
# URL: RandomTechMinutia.wordpress.com
# Date: 6/18/2012
# Updated: 7/2/2012
# Version: 0.4
#
# Purpose:
# Scan and report on Snapshots that are older than 1 day in VCenter
#=====================================================================================================#

#Add Snapins
Add-PSSnapin VMware.VimAutomation.Core

#Get todays Date
$date = Get-Date

#===========================#
#User Adjustable Variables  #
#===========================#

#Name of your Company for Report Title
$CompanyName="CompanyName"

#Vcenter Logon Info
$ESXserver = "vcenterserver.yourdomain.com"
$ESXuser = "vmwareuser"
$ESXpassword = "password"

#Adjust report +/- age of Snap Shots in days
$TimeinDays = 1

#Mail Server Variables
$SMTPserver = "mail.yourdomain.com"
$from = "VMwareReporter@yourdomain.com"
$to = "admin@yourdomain.com"

#===========================#
#Main Script                #
#===========================#

#Determine if already connected to VCenter and if not connect
if ( $DefaultVIServers.Length -lt 1 )
{
Connect-VIServer -Server $ESXserver -User $ESXuser -Password $ESXpassword -WarningAction SilentlyContinue | Out-Null
}

# Format html report
$htmlReport = @"
<style type='text/css'>
.heading {
color:#0B1ABF;
font-size:14.0pt;
font-weight:700;
font-family:Verdana, sans-serif;
text-align:left;
vertical-align:middle;
height:20.0pt;
width:416pt;
}
.colnames {
color:white;
font-size:10.0pt;
font-weight:700;
font-family:Tahoma, sans-serif;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#730000;
}
.text {
color:windowtext;
font-size:10.0pt;
font-family:Arial;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#EAEAEA;
}
.textlate {
color:#730000;
font-size:10.0pt;
font-family:Arial;
font-weight:bold;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#FFFF00;
}
</style>
<table border=0 cellpadding=.5 cellspacing=0 width=556
style='border-collapse:collapse;table-layout:auto;width:600pt'>
<tr style='height:15.0pt'>
<th colspan=6 height=40 width=555>
<center>- $CompanyName - </center></th>
</tr>
<th colspan=6 height=40 width=555>
<center> vSphere Snapshots Daily Report </center></th>
</tr>
<tr>
<th>Name</th>
<th>Note</th>
<th>Size (MB)</th>
<th>VM</th>
<th>VM State</th>
<th>Date Created</th>
</tr>
"@

$vmlist = Get-VM -Server $ESXserver
$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

if ($snapshot.created -lt (Get-Date).AddDays(-$TimeinDays))
{
$htmlReport = $htmlReport +
"<tr><td class='textlate'>" + $snapshot.Name + "</td>" +
"<td class='textlate'>" + "Snapshot Over 24 Hours Old" + "</td>" +
"<td class='textlate'>" + $snapshot.SizeMB + "</td>" +
"<td class='textlate'>"+ $vm.Name + "</td>" +
"<td class='textlate'>" + $vm.PowerState + "</td>" +
"<td class='textlate'>" + $snapshot.Created + "</td></tr>"
}
else
{
$htmlReport = $htmlReport +
"<tr><td class='text'>" + $snapshot.Name + "</td>" +
"<td class='text'>" + "Snapshot Less Than 24 Hours Old" + "</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 $ESXserver -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)

Here is what it will look like in a email.

,

2 Comments