{"id":921,"date":"2015-07-27T17:02:23","date_gmt":"2015-07-27T22:02:23","guid":{"rendered":"http:\/\/www.gamescheat.ca\/?p=921"},"modified":"2019-09-04T19:57:39","modified_gmt":"2019-09-05T00:57:39","slug":"vmware-powercli-script-to-list-snapshots-older-than-specific-days","status":"publish","type":"post","link":"http:\/\/www.gamescheat.ca\/?p=921","title":{"rendered":"VMware PowerCLI script to list snapshots older than specific days"},"content":{"rendered":"<div style=\"float: right;\"><div style=\"margin: 15px 15px 15px 15px\";><script type=\"text\/javascript\"><!--\ngoogle_ad_client = \"pub-3319935785736004\";\ngoogle_alternate_color = \"FFFFFF\";\ngoogle_ad_width = 120;\ngoogle_ad_height = 240;\ngoogle_ad_format = \"120x240_as\";\ngoogle_ad_type = \"text_image\";\ngoogle_ad_channel =\"\";\ngoogle_color_border = \"cccccc\";\ngoogle_color_link = \"cc0000\";\ngoogle_color_bg = \"ffffff\";\ngoogle_color_text = \"000000\";\ngoogle_color_url = \"008000\";\n\/\/--><\/script>\n<script type=\"text\/javascript\"\n  src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\n<\/script><\/div><\/div><p>A VMware vSphere PowerCLI script to find and email a list of all VMware guests that have snapshot older than 3 days.<\/p>\n<blockquote><p># List all VM with snapshots<br \/>\n# Run as scheduled task with a user with rights to view all VMs<\/p>\n<p>Add-PSSnapin VMware.VimAutomation.Core<\/p>\n<p>$Age = 3<br \/>\n$strOutFile = \u201cc:\\snapshot-result.htm\u201d<br \/>\ndel $strOutFile<\/p>\n<p># HTML\/CSS style for the output file<br \/>\n$head = \u201c<\/p>\n<style>\"\n$head = $head + \"BODY{background-color:white;}\"\n$head = $head + \"TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}\"\n$head = $head + \"TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}\"\n$head = $head + \"TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}\"\n$head = $head + \"<\/style>\n<p>\u201d<\/p>\n<p># SMTP info<br \/>\n$smtpServer = \u201cmy.smtp.com\u201d<br \/>\n$strFrom = \u201cvcenter1\u201d<br \/>\n$strTo = \u201cemail1@mymail.com, email2@mymail.com\u201d<br \/>\n$strSubject = \u201cSnapshot older than 3 days \u2013 \u201d + (get-date -DisplayHint date)<br \/>\n$strBody = \u201cAttached is the list of Snapshots\u201d<br \/>\n$strMail = \u201c<-H2><u>\u201d + $strSubject + \u201c<\/u><-\/H2>\u201d<br \/>\n#Remove the \u2013 in the above line<\/p>\n<p># List your vCenter servers in quotes separated by commas<br \/>\n$Servers=\u201dvcenter1\u2033,\u201dvcenter2\u2033<br \/>\nforeach ($Server in $Servers){<br \/>\n\tConnect-VIServer $Server<br \/>\n\t$date=Get-Date -uFormat \u201c%Y%m%d%H%M%S\u201d<br \/>\n\t$strSubject = \u201cINV Snapshots older than 3 days \u2013 \u201d + (get-date -DisplayHint date)<\/p>\n<p>\t# Get the list of VM\u2019s<br \/>\n\t\t$myCol = get-vm | get-snapshot | select vm, Created, name, Description, SizeMB, SizeGB, @{N=\u201dDaysOld\u201d; E={((Get-Date) \u2013 $_.Created).Days}} | ? {$_.DaysOld -gt $Age}<\/p>\n<p>\t# Write the output to an HTML file<br \/>\n\tif ($myCol -ne $null){<br \/>\n \t\t$myCol | Sort-Object VM | ConvertTo-HTML -Head $head -Body $Server | out-File $strOutFile -Append<br \/>\n        Clear-Variable myCol<br \/>\n\t}<\/p>\n<p>DisConnect-VIServer -Server * -Force -Confirm:$false<\/p>\n<p>}<\/p>\n<p>if (Test-Path $strOutFile){<br \/>\n# Mail the output file<br \/>\n\t$msg = new-object Net.Mail.MailMessage<br \/>\n\t$att = new-object Net.Mail.Attachment($strOutFile)<br \/>\n\t$smtp = new-object Net.Mail.SmtpClient($smtpServer)<br \/>\n\t$msg.From = $strFrom<br \/>\n\t$msg.To.Add($strTo)<br \/>\n\t$msg.Subject = $strSubject<br \/>\n\t$msg.IsBodyHtml = 1<br \/>\n\t$msg.Body = Get-Content $strOutFile<br \/>\n#\t$msg.Attachments.Add($att)<br \/>\n\t$smtp.Send($msg)<br \/>\n}<\/p>\n<p>ELSE {<br \/>\n# Mail the output file<br \/>\n\t$msg = new-object Net.Mail.MailMessage<br \/>\n\t$smtp = new-object Net.Mail.SmtpClient($smtpServer)<br \/>\n\t$msg.From = $strFrom<br \/>\n\t$msg.To.Add($strTo)<br \/>\n\t$msg.Subject = \u201cSnpshot Older Than 3 Days\u201d<br \/>\n\t$msg.IsBodyHtml = 1<br \/>\n\t$msg.Body = \u201cGreat news :) There are no snapshot older than 3 days\u201d<br \/>\n\t$smtp.Send($msg)<br \/>\n}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A VMware vSphere PowerCLI script to find and email a list of all VMware guests that have snapshot older than 3 days. # List all VM with snapshots # Run as scheduled task with a user with rights to view&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/www.gamescheat.ca\/?p=921\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/921"}],"collection":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=921"}],"version-history":[{"count":5,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/921\/revisions"}],"predecessor-version":[{"id":934,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/921\/revisions\/934"}],"wp:attachment":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=921"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}