Hyper-V Automation through scripts (Virtual Disks)
October 15, 2009 | In: Hyper-V
I have built my fair share of VMs using both Hyper-V Manager and Virtual Machine Manager 2008 (VMM), but as an engineer, I wanted a way to build multiple VMs without the repetitive clicking involved in doing so within the graphical tools. Fortunately, Microsoft enabled Hyper-V to be managed through WMI scripting. With VMM, Microsoft has extended the scripting capability into PowerShell. For this entry, I will focus on WMI,since I always look to understand what shortcut code is doing behind the scenes. Much of the PowerShell scripting to accomplish these tasks are shorter than their WMI counterparts, but hide the details that I want to see and understand.
For those that don’t know, WMI is the method used to manage pretty much anything Microsoft has developed that runs under the Windows Operating System, so it is fitting that we can use it to manage Hyper-V as well. Microsoft has made available several classes in WMI that can be used to manage Hyper-V. A full listing can be found here.
I will not get into scripting the Hyper-V host server build, but will focus on scripting the virtual machine builds on the host. See, I am building up VMs all the time and needed a method for lighting them up quickly. Building a host or multiple host servers is nice, but not really what I would use in my scenario.
In Hyper-V, there are four primary components that make up a virtual machine. These are virtual disk resources, processor resources, memory resources, and network resources. I will be covering each of these parts separately with a final script that takes everything and merges them into one script that will build a virtual machine for you.
The first thing we need to do before we can play with virtual disks is to create them. Generally, this is pretty simple with a couple of lines.
$VirtDiskSvc = Get-WmiObject -Class "Msvm_ImageManagementService" -Namespace "root\virtualization" $VirtDiskSvc.CreateFixedVirtualHardDisk("f:\VHDs\win2k8.vhd", 20GB)
5 Responses to Hyper-V Automation through scripts (Virtual Disks)
Hyper-V Automation through scripts (Processor) – davidramthun.com
October 16th, 2009 at 11:01 am
[...] Last time I covered automatic Hyper-V through WMI for virtual disks. Now, I want to cover how to automate the creation of processor resources. One thing you will notice is that the pattern used to create virtual disks through WMI will be very similar for creating the rest of our resources. As we figure out how to manipulate settings of virtual machine components, it becomes easier to build a complete script [...]
Dave
October 29th, 2009 at 1:58 am
I can’t seem to get this script to work, keeps complaining about invalid query and invalid namespace. Could you send me the script directly, I can’t seem to copy it from your site above, there is no way og highlighting the text.
Thanks
Dave
David Ramthun
November 4th, 2009 at 10:05 pm
Hmm, it looks like the right script is there, however, some of the formatting went goofy. Some lines that should be continuous are broken. I have corrected the post so that the lines that should be together are all together. Thanks for pointing that out.
Hyper-V Automation through scripts (Final Script) – davidramthun.com
December 7th, 2009 at 3:00 pm
[...] so far we have created, modified, and added resources (memory, processor, disks, and network) to virtual machines. Now, we want to take that information and build a complete [...]
virtual disks - StartTags.com
February 2nd, 2010 at 11:40 am
[...] in Windows Explorer so that you can right-click on a file to mount it as a virtual disk drive. …Hyper-V Automation through scripts (Virtual Disks …I have built my fair share of VMs using both Hyper-V Manager and Virtual Machine Manager 2008 (VMM), [...]