Find VM’s with Snapshots using VMware PowerCLI

After all the hassle I’ve had with snapshots recently it was time to finalise this script. It’ll find all of the snapshots in your environment, based on age, and then e-mails a nice list to you.


param ($Age = 0)
$outfile = "c:\snaps.csv"
$output="VM Name,Snapshot Name,Snapshot Size,Creation Date"+"`n"+"`n"
$vcenter = "servername"

Connect-VIServer $vcenter

$snapshotlist = get-snapshot -VM (get-vm)
Write-Host -ForegroundColor Red "Matching Snapshots Found: "

foreach ($snap in $snapshotlist) {
     if ($snap.Created -lt (Get-Date).AddDays(-$Age)) {
          Write-Host "VM: " $snap.VM "Name: " $snap.Name "Size: " $snap.SizeMB "Created: " $snap.created
          $output = $output + $snap.VM + "," + $snap.Name + "," + $snap.SizeMB + "," + $snap.created + "`n"
          }
     }

disconnect-viserver * -force:$true -confirm:$false

Out-file $output $outfile

#Sets the mail values
$FromAddress = &quot;<;a href=&quot;mailto:Snapshots@domain.com&quot;>;snapshots@domain.com<;/a>;&quot;
$ToAddress = &quot;<;a href=&quot;mailto:recipient@domain.com&quot;>;recipient@domain.com<;/a>;&quot;
$MessageSubject = &quot;Snapshot Report&quot;
$SendingServer = &quot;mail_relay.domain.com&quot;
$MessageBody = $output

#Create the mail message
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody

#Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)

Modify these settings as required for your environment/needs:

$FromAddress = “snapshots@domain.com”
$ToAddress = “recipient@domain.com”
$MessageSubject = “Snapshot Report”
$SendingServer = “mail_relay.domain.com”
$outfile = “c:\snaps.csv”
$vcenter = “servername”

By default, the script will collect info about all snapshots. If you want to modify that list, and only have snapshots created more than 7 days ago, just call the script with the “-age” parameter, and you’ll only get a list of VM’s that have been around for longer than that time.

Onswipe added to Spug.co.uk

Just a quick update for all you iPad users out there, I’ve now added OnSwipe to spug.co.uk which makes browsing the site via iPad a heck of a lot easier – and it looks good too!

For anyone who has an iPad and wants to take a look, just head to the main http://www.spug.co.uk page, and you’ll see the OnSwipe version of the site.