{"id":2729,"date":"2013-10-15T00:01:00","date_gmt":"2013-10-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/10\/15\/recovering-virtual-machines-in-hyper-v-server-2012-r2-part-2\/"},"modified":"2013-10-15T00:01:00","modified_gmt":"2013-10-15T00:01:00","slug":"recovering-virtual-machines-in-hyper-v-server-2012-r2-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/recovering-virtual-machines-in-hyper-v-server-2012-r2-part-2\/","title":{"rendered":"Recovering Virtual Machines in Hyper-V Server 2012 R2: Part 2"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Use Hyper-V cmdlets to identify missing network configurations in virtual machines.<\/span><\/p>\n<p align=\"left\"><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! This week we are working on some business continuity and disaster recovery practices. One of the documents we are building involves how to rebuild a Hyper-V virtual machine in our Windows Server&nbsp;2012&nbsp;R2 environment. Can you tell me if there is a way to identify the configuration data for Hyper-V, specifically the network names in our virtual switches?<\/p>\n<p>&mdash;BR<\/p>\n<p><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 BR,<\/p>\n<p>Honorary Scripting Guy, Sean Kearney here&mdash;filling in for our good friend Ed. He&rsquo;s still digging around with some old scripts he found in the back room.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;This is the second post in a series. If you missed Part 1, see&nbsp;<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>.<\/p>\n<p>Hyper-V Server&nbsp;2012 and Hyper-V Server&nbsp;2012&nbsp;R2 include possibly the coolest cmdlet ever. It&rsquo;s called <strong>Compare-VM<\/strong>. <strong>Compare-VM<\/strong> has a simple task. Look at a virtual machine&rsquo;s configuration and see what&rsquo;s missing from the Hyper-V box.<\/p>\n<p>We used to sing a song like that as a kid when I was in school, &ldquo;Which of these things is not like the other?&rdquo; Well, that&rsquo;s what <strong>Compare-VM<\/strong> does.<\/p>\n<p>So let&rsquo;s pretend that we spun up a brand new Hyper-V Server&nbsp;2012&nbsp;R2 box. We have copied the .xml files, VHDs, and all the configuration files from a previous Hyper-V virtual machine.<\/p>\n<p>In Windows Server&nbsp;2008&nbsp;R2, if you did not export it in advance, you could get away with re-creating the virtual machine and re-attaching the VHD file. But if you didn&rsquo;t have the network settings, the virtual machine would have to detect the network card, which meant the usual game of:<\/p>\n<ul>\n<li>Hope you have cached credentials.<\/li>\n<li>Cross your fingers that it wasn&rsquo;t a domain controller.<\/li>\n<li>Wait&hellip;wait&hellip;wait&hellip;<\/li>\n<\/ul>\n<p>But with <strong>Compare-VM<\/strong>, I can point to the file and obtain what&rsquo;s missing on my Hyper-V box.<\/p>\n<p>So let&rsquo;s look at a sample virtual machine. I literally used a Robocopy to bring it to my brand new Hyper-V&nbsp;2012&nbsp;R2 Server box.<\/p>\n<p>I&rsquo;ve just run <strong>Compare-VM<\/strong> against an .xml file, and I get this output:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4454.1.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4454.1.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Notice the <strong>Incompatibilities <\/strong>property? Let&rsquo;s access that property to see what it has to say for us. We see a message that reveals why the machine couldn&rsquo;t connect:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0211.2.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0211.2.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>If we expand the message, we&rsquo;ll find a more revealing answer:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3755.3.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3755.3.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>This means that the reason I can&rsquo;t import this virtual machine and &ldquo;make it work&rdquo; on a new box is as simple as &ldquo;the virtual network switch with the explicit name PublicNetwork does not exist.&rdquo;<\/p>\n<p>But rather than pulling down 150 individual virtual machines, individually accessing their configurations, and trying to pull the network names, we can simply script it.<\/p>\n<p>We&rsquo;re going to leverage the .csv file for this script. So ideally, we could have virtual machines that we recovered from a back-up or that we switched to a lab, and then pull out the missing network data:<\/p>\n<p style=\"padding-left: 30px\">$HyperVList=IMPORT-CSV C:\\HyperVList.csv<\/p>\n<p style=\"padding-left: 30px\">$ImportErrors=Foreach ($Machine in $HyperVList)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $VMError=(Compare-VM &ndash;path $Machine.XMLFile).InCompatibilities.Message<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [PSCUSTOMOBJECT]@{Machine=$Machine.Name;Error=$VMError}<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">$ImportErrors | Select-object Error &ndash;Unique<\/p>\n<p>What this should give us is a nice simple list of missing network switches in our virtual machines. With this, we can re-create them by using Windows PowerShell.<\/p>\n<p>But that&rsquo;s a story for tomorrow&hellip;<\/p>\n<p>I 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=\"mailto: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.<\/p>\n<p><strong>Sean Kearney, <\/strong>Honorary Scripting Guy and Windows PowerShell MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Hyper-V cmdlets to identify missing network configurations in virtual machines. &nbsp;Hey, Scripting Guy! This week we are working on some business continuity and disaster recovery practices. One of the documents we are building involves how to rebuild a Hyper-V virtual machine in our Windows Server&nbsp;2012&nbsp;R2 environment. Can you tell me if there is [&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-2729","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 Hyper-V cmdlets to identify missing network configurations in virtual machines. &nbsp;Hey, Scripting Guy! This week we are working on some business continuity and disaster recovery practices. One of the documents we are building involves how to rebuild a Hyper-V virtual machine in our Windows Server&nbsp;2012&nbsp;R2 environment. Can you tell me if there is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2729","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=2729"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2729\/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=2729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}