{"id":151,"date":"2019-11-19T20:20:29","date_gmt":"2019-11-19T20:20:29","guid":{"rendered":"http:\/\/192.168.8.14\/?p=151"},"modified":"2020-03-29T16:18:29","modified_gmt":"2020-03-29T15:18:29","slug":"snapshots","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=151","title":{"rendered":"Powershell Tutorial Dealing with Snapshots"},"content":{"rendered":"\n<p>Snapshots, if left undetected will grow and consume space as well as slow your VMs down if you have enough of them. This is an endless battle for the system administrator as (in my experience) developers will want to keep snapshots forever. This often leads to the same \u201csnapshots are not backups\u201d conversation.<\/p>\n\n\n\n<p>In this post I will detail how to detect and deal with VMWare snapshots using powershell.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How many snapshots do I have?<\/h3>\n\n\n\n<p>To get a quick view of snapshots in you environment just issue this command.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nget-vm | get-snapshot\n<\/pre><\/div>\n\n\n<p> You will get a very helpful output of the name of the snapshot and ts description (usually empty) <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"459\" height=\"133\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/11\/snap101-1.png\" alt=\"\" class=\"wp-image-155\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap101-1.png 459w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap101-1-300x87.png 300w\" sizes=\"auto, (max-width: 459px) 100vw, 459px\" \/><\/figure>\n\n\n\n<p>Not much use. to see what attributes the snapshot object has we just pump that in to a get-member cmdlet (or GM)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nget-vm | get-snapshot | gm\n<\/pre><\/div>\n\n\n<p>This will give us the attributes of the snapshot object so we can hopfuly get something more helpful.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"810\" height=\"485\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/11\/snap102.png\" alt=\"\" class=\"wp-image-156\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap102.png 810w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap102-300x180.png 300w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap102-768x460.png 768w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/figure>\n\n\n\n<p>mmmm vm, SizeGB and created look useful so we can re-write the first command and select those attributes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nget-vm | get-snapshot | select VM, name, Created, SizeGB\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"503\" height=\"136\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/11\/snap103.png\" alt=\"\" class=\"wp-image-157\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap103.png 503w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap103-300x81.png 300w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/figure>\n\n\n\n<p>Much more helpful. We have the VM name, creation date and the size.<\/p>\n\n\n\n<p>We can make it more useful by sorting by size or created date.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nget-vm | get-snapshot | select VM, name, Created, SizeGB | sort sizeGB -Descending\nget-vm | get-snapshot | select VM, name, Created, SizeGB | sort created\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"746\" height=\"295\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/11\/snap104.png\" alt=\"\" class=\"wp-image-158\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap104.png 746w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap104-300x119.png 300w\" sizes=\"auto, (max-width: 746px) 100vw, 746px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">What Snapshots can I delete?<\/h3>\n\n\n\n<p>Deciding what snapshots to delete is up to you. but as a guide here I will assume any snapshot called temp can go, and anything created more then half a day ago.<br>In a production environment probably anything older then 120 days could go. but for this example anything older then half a day is going to be deleted.<\/p>\n\n\n\n<p>So this time instead of using a select cmdlet we use the where-object so our list of snapshots is only what we want to delete.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nget-vm | get-snapshot | where{$_.name -eq &quot;temp&quot;}\n<\/pre><\/div>\n\n\n<p>or lets get old snapshots<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nget-vm | get-snapshot | where{$_.created -lt (get-date).adddays(-0.5)}\n<\/pre><\/div>\n\n\n<p>The above two bits of code will give me.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"198\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/11\/snap105.png\" alt=\"\" class=\"wp-image-159\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap105.png 685w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/11\/snap105-300x87.png 300w\" sizes=\"auto, (max-width: 685px) 100vw, 685px\" \/><\/figure>\n\n\n\n<p>Oh course we can pipe that in to the same select statement we used earlier to get the VM name, size and creation date.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Deleting snapshots.<\/h3>\n\n\n\n<p>Now we have identified the snapshots we want to delete we just simply pipe the above command in to the remove-snapshot cmdlet.<br>The following switches are useful when using remove-snapshot<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>-WhatIf will pretend to delete snapshots but not really delete anything, handy for testing.<\/li><li>-confirm:$true This is the default, Powershell will prompt for every snapshot deletion.<\/li><li>-confirm:$false When you are happy you are deleting the correct snapshots and you dont want to confirm 100 deletions, use this.<\/li><li>-RunAsync don\u2019t wait for the snapshot to be deleted. Not recommended if you are deleting lots of snapshots.<\/li><\/ul>\n\n\n\n<p>The cautious would use this to remove all snapshots older then 120 days and be prompted for each snapshot.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nget-vm | get-snapshot | where{$_.created -lt (get-date).adddays(-120)} | Remove-Snapshot -confirm:$true\n\n<\/pre><\/div>\n\n\n<p>\n\nWhile the more gun-ho administrators would run the following to remove all snapshots called \u201ctemp\u201d with no prompting.\n\n<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nget-vm | get-snapshot | where{$_.name -eq &quot;temp&quot;} | Remove-Snapshot -confirm:$false\n\n<\/pre><\/div>\n\n\n<p>\n\nWell, that\u2019s all there is to detecting and removing snapshots. I hope the above helps someone out. And remember \u201cSnapshots are not backups!\u201d\n\n<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Snapshots, if left undetected will grow and consume space as well as slow your VMs down if you have enough of them. This is an endless battle for the system administrator as (in my experience) developers will want to keep snapshots forever. This often leads to the same \u201csnapshots are not backups\u201d conversation. In this&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5,27],"tags":[12,28],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-powercli","category-tutorial","tag-powercli","tag-snapshots"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151"}],"version-history":[{"count":3,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":231,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions\/231"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}