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 = "<;a href="mailto:Snapshots@domain.com">;snapshots@domain.com<;/a>;" $ToAddress = "<;a href="mailto:recipient@domain.com">;recipient@domain.com<;/a>;" $MessageSubject = "Snapshot Report" $SendingServer = "mail_relay.domain.com" $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.