Learn about Windows PowerShell
Summary: Use Hyper-V cmdlets to identify missing network configurations in virtual machines.
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 2012 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?
Honorary Scripting Guy, Sean Kearney here—filling in for our good friend Ed. He’s still digging around with some old scripts he found in the back room.
Note This is the second post in a series. If you missed Part 1, see Recovering Virtual Machines in Hyper-V Server 2012 R2: Part 1.
Hyper-V Server 2012 and Hyper-V Server 2012 R2 include possibly the coolest cmdlet ever. It’s called Compare-VM. Compare-VM has a simple task. Look at a virtual machine’s configuration and see what’s missing from the Hyper-V box.
We used to sing a song like that as a kid when I was in school, “Which of these things is not like the other?” Well, that’s what Compare-VM does.
So let’s pretend that we spun up a brand new Hyper-V Server 2012 R2 box. We have copied the .xml files, VHDs, and all the configuration files from a previous Hyper-V virtual machine.
In Windows Server 2008 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’t have the network settings, the virtual machine would have to detect the network card, which meant the usual game of:
But with Compare-VM, I can point to the file and obtain what’s missing on my Hyper-V box.
So let’s look at a sample virtual machine. I literally used a Robocopy to bring it to my brand new Hyper-V 2012 R2 Server box.
I’ve just run Compare-VM against an .xml file, and I get this output:
Notice the Incompatibilities property? Let’s access that property to see what it has to say for us. We see a message that reveals why the machine couldn’t connect:
If we expand the message, we’ll find a more revealing answer:
This means that the reason I can’t import this virtual machine and “make it work” on a new box is as simple as “the virtual network switch with the explicit name PublicNetwork does not exist.”
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.
We’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:
$ImportErrors=Foreach ($Machine in $HyperVList)
$VMError=(Compare-VM –path $Machine.XMLFile).InCompatibilities.Message
$ImportErrors | Select-object Error –Unique
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.
But that’s a story for tomorrow…
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Sean Kearney, Honorary Scripting Guy and Windows PowerShell MVP
Good post. Useful.
Note that Compare-VM is also in Windows 8.
Please could you tell us about how to recover disks?
After restarting Cluster services I found my CSV3 data moved to CSV5 so VM not booting as It cannot find the disk.
Any idea or script?
I'm getting a error on $ImportErrors=Foreach ($Machine in $HyperVList) ...it says "unexpected token 'in' in expression or statement.
The most easiest way to recover data from Virtual machine with the help of this application.
VHD is a file format of virtual machine. I also have a .vhd file but due to some reasons the data of my VHD got corrupted and I needed to fix it.Unfortunately I also didn’t have a fresh backup of VHD. Then I found a superb Tool named VHD data recovery
tool Unfortunately I also didn’t have a fresh backup of VHD. After using its free demo I actually got all my data back along with deleted VHD file items. I followed this URL & get the excellent application.
Read More: http://www.filesrecoverytools.com/vhd-data-recovery.html
Recover corrupt vhd file from Kernel for VHD File Recovery Software. It is capable of resolving all sorts of corruption issues in Virtual Hard Disk. I would like to refer you to
VHD Software is an advance and professional application and this tool is able to images, documents ,attachments, zipped etc from corrupt VHD. It is effective and easy to use application.For more information and free download click