I’ve been using PowerShell quite a bit in the past few months for various tasks, such as mass Storage vMotion, snapshot reporting as well as some other tasks and have found it to be invaluable and I learn more every day. It reminds me of AppleScript, but much more useful. Since nearly everything MS related has cmdlets, it is worth learning and sharing. Lately, I’ve been wanting to learn Git for version control so I figured theres no better time to learn. I’ve started a GitHub repository that I plan on using to share my scripts and bits. I’ll most likely move my other “projects” into GitHub too as I continue to learn how to manage it. So without further ado here is the vm-powershell script repository and you can follow me (rabbitofdeath) too!

I’ve come up with this Windows PowerShell script to help identify virtual machines that have snapshots older than 1 day. It generates a CSV file and emails specified recipients. This is particularly handy in large VMware environments where snapshots can cause major havoc on datastore utilization. Enjoy!


# ==============================================================================================
# NAME: vmwaresnapreport.ps1
# AUTHOR: Eric Kiel erickielcollideoscopeorg
# DATE  : 4/27/2012
# COMMENT: This script will connect to a VMware Virtual Center server and report on VMs with snapshots
# that are older than 1 day.
# VERSION: 1.0 - Original Code
# USAGE: .\vmwaresnapreport.ps1
# Variables: 
# $vcenter - vCenter host name
# $vcenterdomain - domain that vCenter resides in; yourdomain.com
# $mailserver - SMTP Mail relay
# $email1 - Distribution list or email address to send report to
# $email2 - 2nd distribution list or email address for reporting
# REQUIREMENTS: VMware vSphere PowerCLI
# ==============================================================================================

#Initialize Variables
$vcenter = "YOUR VCENTER HOST NAME"
$vcenterdomain = "VCENTER DOMAIN"
$mailserver = "YOUR SMTP SERVER"
$email1 = "YOUR DISTRIBUTION LIST OR EMAIL ADDRESS"
$email2 = "DISTRIBUTION LIST 2 OR ANOTHER EMAIL ADDRESS"

# Enable VMware Snap-in
add-pssnapin VMware.VimAutomation.Core

$date = get-date -format MM-dd-yyyy
connect-viserver $vcenter
Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-1)} | select VM, Name, Description, Created, PowerState, SizeMB | export-csv $date-$vcenter-snapshots.csv -notypeinformation
send-mailmessage -from "$vcenter@$vcenterdomain" -to "$email1", "$email2" -subject "$vcenter Snapshot Report for $date" -attachment "$date-$vcenter-snapshots.csv" -body "Snapshot report for $vcenter generated on $date" -smtpserver $mailserver

Download vmwaresnapreport and rename to .ps1 to run with PowerShell.