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.

Share

Recent Posts

Microsoft

McCann & MullenLowe – Microsoft Teams Case Study

When bringing companies with different technology systems together, it can be difficult to efficiently collaborate. #Microsoft Teams can help. Learn how its single platform system provided McCann and MullenLowe with the solution they needed to enable their employees to work together. Check out this video:

Read More »