{"id":2005,"date":"2018-01-03T00:38:48","date_gmt":"2018-01-03T00:38:48","guid":{"rendered":"http:\/\/obieta.com\/?p=2005"},"modified":"2018-01-03T00:38:48","modified_gmt":"2018-01-03T00:38:48","slug":"how-to-automatically-rotate-hyperv-snapshots","status":"publish","type":"post","link":"https:\/\/obieta.com\/?p=2005","title":{"rendered":"How to Automatically Rotate HyperV Snapshots"},"content":{"rendered":"<div class=\"row\">\n<div class=\"article-content col-lg-12 col-md-12 col-sm-12\">\n<div class=\"h1-subtitle h1-subtitle-pad-bottom\">The following instructions can be used to set up a rotating snapshot schedule for all the Virtual Machines that are running in Hyper-V on the server. The default rotation for this script is 7 days which can be modified depending on your preference.<\/div>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"article-content col-lg-8 col-md-8 col-sm-8\">\n<div><span style=\"font-family: Arial, Verdana; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: Arial, Verdana; font-size: small;\"><b>Warning<\/b><\/span><\/div>\n<div><span style=\"font-family: Arial, Verdana; font-size: small;\">In order for this script to run successfully, you will need to make sure you have additional space on your hard drive to hold the snapshot history.<\/span><\/div>\n<div><span style=\"font-family: Arial, Verdana; font-size: small;\"><span style=\"font-size: small;\">Each snapshot consists of RAM snapshots as well as changes made to the virtual hard drive since the last snapshot. \u00a0This means that if you have a VM with 4GB of RAM and you want 7 days of snapshots, you will need a minimum of 7days*4GB of RAM = 28GB of additional disk space in order to\u00a0<\/span>accommodate<span style=\"font-size: small;\">\u00a0a VM that has completely static data. \u00a0In actuality, it is not unreasonable to plan for up to 4 times this amount of disk space to\u00a0accommodate\u00a0hard drive data changes.<\/span><\/span><\/div>\n<div><span style=\"font-family: Arial, Verdana; font-size: small;\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: Arial, Verdana; font-size: small;\"><b>Create the Snapshot Script<\/b><\/span><\/div>\n<ol>\n<li>Log into your server through Remote Desktop. For instructions on how to login to your server,<\/li>\n<li>Run\u00a0<b>PowerShell\u00a0<\/b>from the Start menu or your desktop.<\/li>\n<li>Run the following script.\n<ol class=\"linenums\">\n<li class=\"L0\"><span class=\"kwd\">set<\/span><span class=\"pun\">&#8211;<\/span><span class=\"pln\">executionpolicy\u00a0<\/span><span class=\"typ\">RemoteSigned<\/span><\/li>\n<\/ol>\n<\/li>\n<li>When the Execution Policy Change warning is displayed enter\u00a0<b>Y<\/b>.<\/li>\n<li>Open Notepad and paste the following contents into it. (Note line 1 where you can change the number of days for your rotation):\n<ol class=\"linenums\">\n<li class=\"L0\"><span class=\"pln\">$Days\u00a0<\/span><span class=\"pun\">=<\/span><span class=\"pln\">\u00a0<\/span><span class=\"lit\">7<\/span><span class=\"pln\">\u00a0<\/span><span class=\"com\">#change this value to determine the number of days to rotate.<\/span><\/li>\n<li class=\"L1\"><\/li>\n<li class=\"L2\"><span class=\"pln\">$VMs\u00a0<\/span><span class=\"pun\">=<\/span><span class=\"pln\">\u00a0<\/span><span class=\"typ\">Get<\/span><span class=\"pun\">&#8211;<\/span><span class=\"pln\">VM<\/span><\/li>\n<li class=\"L3\"><span class=\"kwd\">foreach<\/span><span class=\"pun\">(<\/span><span class=\"pln\">$VM\u00a0<\/span><span class=\"kwd\">in<\/span><span class=\"pln\">\u00a0$VMs<\/span><span class=\"pun\">){<\/span><\/li>\n<li class=\"L4\"><span class=\"pln\">\u00a0 \u00a0 $Snapshots\u00a0<\/span><span class=\"pun\">=<\/span><span class=\"pln\">\u00a0<\/span><span class=\"typ\">Get<\/span><span class=\"pun\">&#8211;<\/span><span class=\"typ\">VMSnapshot<\/span><span class=\"pln\">\u00a0$VM<\/span><\/li>\n<li class=\"L5\"><\/li>\n<li class=\"L6\"><span class=\"pln\">\u00a0 \u00a0\u00a0<\/span><span class=\"kwd\">foreach<\/span><span class=\"pun\">(<\/span><span class=\"pln\">$Snapshot\u00a0<\/span><span class=\"kwd\">in<\/span><span class=\"pln\">\u00a0$Snapshots<\/span><span class=\"pun\">){<\/span><\/li>\n<li class=\"L7\"><\/li>\n<li class=\"L8\"><span class=\"pln\">\u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span><span class=\"kwd\">if<\/span><span class=\"pln\">\u00a0<\/span><span class=\"pun\">(<\/span><span class=\"pln\">$snapshot<\/span><span class=\"pun\">.<\/span><span class=\"typ\">CreationTime<\/span><span class=\"pun\">.<\/span><span class=\"typ\">AddDays<\/span><span class=\"pun\">(<\/span><span class=\"pln\">$Days<\/span><span class=\"pun\">)<\/span><span class=\"pln\">\u00a0<\/span><span class=\"pun\">&#8211;<\/span><span class=\"pln\">lt\u00a0<\/span><span class=\"pun\">(<\/span><span class=\"kwd\">get<\/span><span class=\"pun\">&#8211;<\/span><span class=\"pln\">date<\/span><span class=\"pun\">)){<\/span><\/li>\n<li class=\"L9\"><span class=\"pln\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span><span class=\"typ\">Remove<\/span><span class=\"pun\">&#8211;<\/span><span class=\"typ\">VMSnapshot<\/span><span class=\"pln\">\u00a0$Snapshot<\/span><\/li>\n<li class=\"L0\"><span class=\"pln\">\u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span><span class=\"pun\">}<\/span><span class=\"pln\">\u00a0<\/span><\/li>\n<li class=\"L1\"><span class=\"pln\">\u00a0 \u00a0\u00a0<\/span><span class=\"pun\">}<\/span><\/li>\n<li class=\"L2\"><\/li>\n<li class=\"L3\"><span class=\"pln\">\u00a0 \u00a0\u00a0<\/span><span class=\"typ\">Checkpoint<\/span><span class=\"pun\">&#8211;<\/span><span class=\"pln\">VM $VM<\/span><\/li>\n<li class=\"L4\"><span class=\"pun\">}<\/span><\/li>\n<\/ol>\n<\/li>\n<li>\u00a0Save the file as\u00a0<b>C:\\SnapshotVMs.ps1<\/b><\/li>\n<li>Open\u00a0<b>Task Scheduler\u00a0<\/b>(press your\u00a0<b>Windows\u00a0<\/b>key and start typing\u00a0<b>Task Scheduler<\/b>).<\/li>\n<li>\n<div><span style=\"font-family: Arial, Verdana; font-size: small;\">In the\u00a0<b>Actions<\/b>\u00a0section click\u00a0<b>Create Basic Task<\/b>\u00a0and follow the wizard to set up the following parameters:<\/span><\/div>\n<div>\n<ul>\n<li>Name: \u00a0<b>CUSTOM &#8211; SNAPSHOTVMS &gt; Next<\/b><\/li>\n<li>Trigger: \u00a0<b>Daily &gt; Next<\/b><\/li>\n<li>Daily: select\u00a0<b>11:00:00 PM<\/b>\u00a0Recur every:\u00a0<b>1\u00a0days<\/b>\u00a0&gt;\u00a0<b>Next<\/b><b>.<\/b><\/li>\n<li>Action: \u00a0<b>Start a program &gt; Next.<\/b><\/li>\n<li>Program\/Script: \u00a0type\u00a0<b>PowerShell<\/b><\/li>\n<li>Add Arguments: \u00a0type\u00a0<b>C:\\SnapshotVMs.ps1\n<p><\/b><\/li>\n<\/ul>\n<\/div>\n<\/li>\n<li>Check the box for &#8220;<b>Open the Properties dialog for this task when I click Finish<\/b>&#8221; and click\u00a0<b>Finish<\/b>.<\/li>\n<li>In the\u00a0<b>General<\/b>\u00a0tab, in the Security Options section, select the option to\u00a0<b>Run whether user is logged on or not<\/b><b>.\u00a0<\/b>Click\u00a0<b>OK<\/b><\/li>\n<li>Enter the\u00a0<b>Administrator<\/b>\u00a0credentials that you use to log into your server. Click\u00a0<b>OK.<\/b><\/li>\n<li>In\u00a0<b>Task Scheduler<\/b>\u00a0click on\u00a0<b>Task Scheduler Library<\/b>.<\/li>\n<li>Locate and right click on the\u00a0<b>CUSTOM &#8211; SNAPSHOTVMS<\/b>\u00a0task you created.<\/li>\n<li><b>Run\u00a0<\/b>the scheduled task to verify that it executes correctly. If successful, you will see a PowerShell console open and a snapshot created for all servers in\u00a0<b>Hyper-V Manager<\/b>.<\/li>\n<li>Log in the following day to verify that all VMs on the server have a snapshot on them in Hyper-V Manager.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The following instructions can be used to set up a rotating snapshot schedule for all the Virtual Machines that are running in Hyper-V on the server. The default rotation for this script is 7 days which can be modified depending on your preference. \u00a0 Warning In order for this script to run successfully, you will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/posts\/2005"}],"collection":[{"href":"https:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obieta.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2005"}],"version-history":[{"count":0,"href":"https:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/posts\/2005\/revisions"}],"wp:attachment":[{"href":"https:\/\/obieta.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obieta.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obieta.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}