{"id":33,"date":"2015-01-24T19:16:15","date_gmt":"2015-01-25T00:16:15","guid":{"rendered":"http:\/\/packetlost.com\/blog\/?p=33"},"modified":"2017-02-07T21:41:10","modified_gmt":"2017-02-08T02:41:10","slug":"vmware-email-report-of-old-snapshots","status":"publish","type":"post","link":"https:\/\/packetlost.com\/blog\/2015\/01\/24\/vmware-email-report-of-old-snapshots\/","title":{"rendered":"Old Snapshots &#8211; PowerCLI Email Report"},"content":{"rendered":"<p>Whether your developers are keeping snapshots around for months or your backup program is leaving behind old snapshots due to backup failures, its always good to know about your aging snapshots.<\/p>\n<p>Below is a script which will find all VMs with snapshots older than 7 days. As usual you can customize this to meet your needs, for me I like to keep transcripts and output files for archival and troubleshooting.<\/p>\n<pre class=\"lang:ps decode:true  \">$currentdate = (Get-Date).tostring(\"yyyy-MM-dd\")\r\n$transfilename = 'OldSnapsTranscript_' + $currentdate + '.txt'\r\n$transfilepath = 'C:\\scripts\\Transcripts\\' + $transfilename\r\nStart-Transcript $transfilepath -NoClobber -Append\r\n\r\n#Load PowerCLI snapins and dot source them\r\nAdd-PSSnapin vmware.vimautomation.core\r\n. \"C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\Scripts\\Initialize-PowerCLIEnvironment.ps1\"\r\n\r\n\r\nConnect-VIServer vcenterserver.company.com\r\n\r\n#Generating timestamped log name for powercli html output\r\n$7daysago = (Get-Date).AddDays(-7)\r\n$outfilename = 'Old_Snaps_' + $currentdate + '.htm'\r\n$outfilepath = 'C:\\scripts\\Output\\' + $outfilename\r\n\r\n\r\n$vmswithstalesnaps = Get-VM |\r\nGet-Snapshot | Where-Object {$_.created -lt $7daysago } |\r\nSelect-Object vm,Name,Created,@{Name=\"MbSize\";Expression={[math]::Truncate($_.SizeMb)}}\r\n\r\n#Convert it to html and save the output if it exists\r\nIf ($vmswithstalesnaps) { $vmswithstalesnaps | Sort-Object created | ConvertTo-Html | Out-File $outfilepath }\r\n\r\n\r\n\r\n\r\n#variables for email output\r\n$eto = \"youremail@company.com\"\r\n$efrom = \"PowerCLI_Reports &lt;PowerCLIReports@company.com&gt;\"\r\n$esubject = \"Virtual Machine Stale SnapShot Report\"\r\nIf ($vmswithstalesnaps) { $ebody = (Get-Content $outfilepath | Out-String) }\r\n$eserver = \"smtpserver.company.com\"\r\n\r\n#check the variable for a value to determine which email to send\r\nIf ($vmswithstalesnaps) { Send-MailMessage -From $efrom -To $eto -Subject $esubject -BodyAsHtml -Body $ebody -smtpserver $eserver }\r\nElse { Send-MailMessage -From $efrom -To $eto  -Subject $esubject -Body \"There are no VMs with snapshots older than 7 days in this report\" -smtpserver $eserver }\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whether your developers are keeping snapshots around for months or your backup program is leaving behind old snapshots due to backup failures, its always good to know about your aging snapshots. Below is a script which will find all VMs &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/packetlost.com\/blog\/2015\/01\/24\/vmware-email-report-of-old-snapshots\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,16],"tags":[20,18,3,19,17],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-powercli","category-vmware","tag-automation","tag-powercli","tag-powershell","tag-snapshots","tag-vmware"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":4,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions\/43"}],"wp:attachment":[{"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}