19 June 2011

As part of a small project I make starting and stopping of Hyper-V machines using WMI and C#

Starting virtual machine using WMI and C#

A few weeks ago one of my friends showed me an impressive way how customers can evaluate software without trial version installation. Nowadays people value their time so much that they are not keen to install something unless they are sure it's right for them. So why not give the access to the virtual machine for a short period of time, let the customer play with the software and then revert everything back, ready for another prospect? 

There's nothing particularly sophisticated about this, but not many of the vendors I know use it. The friend of mine made this all on a Linux box, so I had to find out how to do that on my Windows Server 2008 R2 with Hyper-V installed. Everything is on MSDN, but not on the surface especially for those of us without WMI and Powershell experience. 

So here's how it's done: 

  • Use System.Management namespace
  • Run the code under Administrator priveleges

 

                
ManagementScope scope = new ManagementScope(@"root\virtualization", null);
string q = "select * from Msvm_ComputerSystem Where ElementName = 'Windows Server 2008 R2'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(q));
ManagementObjectCollection VirtualMachines = searcher.Get();
foreach (ManagementObject instance in VirtualMachines)
{
ManagementBaseObject inParams = instance.GetMethodParameters("RequestStateChange");
inParams["RequestedState"] = 2;
                    ManagementBaseObject outParams = instance.InvokeMethod(
                        "RequestStateChange",
                        inParams,
                        null);
break;
}

Of course you might not need to iterate through all virtual machines, but pick the first one, if you're sure that's the one you need. In line 2 'Windows Server 2008 R2' is the name of the virtual machine (displayed in Hyper-V Manager). Finally on line 8 the parameter value 2 is to start VM, 3 - to stop VM. 

From an architecture perspective I would put this code in WCF service that would allow me to start the VM after a certain signal from a web-site is received (e.g. someone filled in the form). It is also a good idea to have a timer restricting the total usage time (may be with a way to extend that time from inside the virtual machine). 


Post a comment


RECENT BLOG ENTRIES
30 November 2013
Bigcommerce template editing
How to change content in BigCommerce.com templates: guide for a complete newbie.
Read full story
09 October 2013
What if Fancybox does not work at all
If Fancybox library does not work, this might be due to the conflicts with other JS libraries and not the syntax error.
Read full story
14 October 2012
Fancybox with ASP.NET form on Umbraco
Using ASP.NET form on Fancybox popup to make login window
Read full story

Blog archive

The author of this web-site supports WWF . Please do your part in saving our planet!

Alex’s expertise in developing and maintaining web applications has been invaluable to the College – J. Wittersheim, Director of Information Management and Funding, Bury College