Windows 2008 server has a Hyper V to manage the Multiple Virtual Machines. The Hyper V manager helps to create Virtual machine and manage them. We can change the VHD and update the path using the Hyper V Manager. There are situation we need to write a program to do those tasks. Example when we do the DR using multiple Virtual Machines, some of the VM VHD path was not mapped exacted to the correct path. The VHD path was still pointing to old server while exporting the VM. So to update the VHD path programmatically i started looking to scripts and programs.
There are several options to write a program for Hyper V.
- PowerShell Script
- WMI Provider Object using C#/VB.NET
PowerShell script is the most widely used approach to program any OS related operation. For Hyper V updating the VHD Path using PowerShell is a straight forward process. Just need to get the VM and the relevant Connections(vhd) and update the path property.
Scripting through VB is similar to WMI programming. VBscript will createObject of WMIProvider dll and call the relevant method.
I need to have UI to update the VHD path so i opted to use WMI Provider. System.Management namespace provides the whole new set of namespaces to manage the OS. Most of the operation which can be done by PowerShell can be done by WMI provider. The details of the provider can be found here http://msdn.microsoft.com/en-us/library/ms751442.aspx.
Basically there is a COM/DCOM based WMI provider and the .NET Based WMI Provider. Depends on the client the WMI provider can be used. In my case my client program will be .NET windows Application so i opted to use .NET WMI provider. WMI provider consist of many namespaces to manage the OS operation. Below are few list of Namespaces taken from my Win 7.
To work with the Hyper V API, there is a separate namespace called root/Virtualization. This namespace provides all function to manage the VM operations. Each namespace has classess. These classes defines the specific operation to the namespace. Ex root/virtualization has classes below. Each class has properties and methods.
To explore the WMI namespaces and classes in the system please use the below tools. This WMI Tool allows the user to explorer all the namespaces available in the system.
To get any information from WMI objects we need to instantiate the WMI object and use the relevant namespace. Also WMI provide the Query provider which can be used to write a SQL kind of query to get the objects using the property. The WMI objects can be compared to a relational DB where we can query the object using a query provider and keys.
Microsoft has a tool to generate a c#, VB or VBScript code. WMI Code Creator lists down all the namespace available in the machine and generates the code based on the selection of operation. Using this tool i have generated the basic code to get the relevant device information.
using this we can create a windows application to update the VHD Path which will be published in the next blog.