{"id":2717,"date":"2013-10-17T00:01:00","date_gmt":"2013-10-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/10\/17\/recovering-virtual-machines-in-hyper-v-server-2012-r2-part-4\/"},"modified":"2013-10-17T00:01:00","modified_gmt":"2013-10-17T00:01:00","slug":"recovering-virtual-machines-in-hyper-v-server-2012-r2-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/recovering-virtual-machines-in-hyper-v-server-2012-r2-part-4\/","title":{"rendered":"Recovering Virtual Machines in Hyper-V Server 2012 R2: Part 4"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use Windows PowerShell to import virtual machines that were not exported.\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! Over the weekend, I rebuilt our Hyper-V Server&nbsp;2012&nbsp;R2 environment. All of the machines and configuration data are on a second drive. I&rsquo;d like to avoid spending all afternoon manually re-creating over 200 virtual machines in our lab. Is there an easier way?\n&mdash;SH\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello SH,\nHonorary Scripting Guy, Sean Kearney here&mdash;again filling in for our good friend Ed.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong>&nbsp;&nbsp;This is the fourth post in a series. To review, see:<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/10\/14\/recovering-virtual-machines-in-hyper-v-server-2012-r2-part-1.aspx\" target=\"_blank\">Recovering Virtual Machines in Hyper-V Server 2012 R2: Part 1<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/10\/15\/recovering-virtual-machines-in-hyper-v-server-2012-r2-part-2.aspx\" target=\"_blank\">Recovering Virtual Machines in Hyper-V Server 2012 R2: Part 2<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/10\/16\/recovering-virtual-machines-in-hyper-v-server-2012-r2-part-3.aspx\" target=\"_blank\">Recovering Virtual Machines in Hyper-V Server 2012 R2: Part 3<\/a><\/li>\n<\/ul>\n<p>With Hyper-V and Windows PowerShell, life certainly got easier in Hyper-V Server&nbsp;2012 and Hyper-V Server&nbsp;2012&nbsp;R2. We can now target an .xml file with the previous Hyper-V configuration (not just an exported one) and import it back into the environment.\nOf course, we have to know where the file is. That&rsquo;s why it was critical to build a list of the .xml files in Part&nbsp;1 of this series. So we have a .csv file from Part 1 that we can access:<\/p>\n<p style=\"padding-left: 30px\">$HyperVlist=IMPORT-CSV C:HyperVList.csv\nWe can now target the XML data in each of these virtual machines to re-import them:<\/p>\n<p style=\"padding-left: 30px\">Foreach ($Machine in $HyperVList)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IMPORT-VM &ndash;path $Machine.Name<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }\nBut what if we forgot something? Maybe the hard disk for a machine was forgotten or is in the wrong location?\nThe cool part is <strong>Import-VM<\/strong> will catch that and spit out an error message that indicates to run <strong>Compare-VM<\/strong> against it. We can trap the virtual machines that didn&rsquo;t import properly by grabbing the errors. We&rsquo;ll actually report failures instead of successes so we know which virtual machines to re-examine.<\/p>\n<p style=\"padding-left: 30px\">Foreach ($Machine in $HyperVList)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $BadVM=$NULL<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IMPORT-VM &ndash;path $Machine.Name &ndash;ErrorVariable BadVM | OUT-NULL<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If ($BADVM) { $Name=$Machine.Name; &ldquo;Failure to import Machine $Name&rdquo;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">}\nYou can then examine the virtual machines that failed by using <strong>Compare-VM<\/strong> as we did previously, but it might be something more detailed, such as the virtual machine exists already or that the VHDs are not in the expected location.\nTomorrow we&rsquo;ll get those virtual machines running and adjust some useful settings that we typically need in a Hyper-V environment&mdash;all without the use of a GUI.\nI invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Sean Kearney<\/strong>, Honorary Scripting Guy and Windows PowerShell MVP&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Windows PowerShell to import virtual machines that were not exported. &nbsp;Hey, Scripting Guy! Over the weekend, I rebuilt our Hyper-V Server&nbsp;2012&nbsp;R2 environment. All of the machines and configuration data are on a second drive. I&rsquo;d like to avoid spending all afternoon manually re-creating over 200 virtual machines in our lab. Is there an [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,271,3,154,45],"class_list":["post-2717","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-hyper-v","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use Windows PowerShell to import virtual machines that were not exported. &nbsp;Hey, Scripting Guy! Over the weekend, I rebuilt our Hyper-V Server&nbsp;2012&nbsp;R2 environment. All of the machines and configuration data are on a second drive. I&rsquo;d like to avoid spending all afternoon manually re-creating over 200 virtual machines in our lab. Is there an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2717"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2717\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}