Start and Stop Hyper-V using .NET application

351965 pts.
Tags:
.NET applications
C
Hyper-V
Virtual Machines
Is there a way for a .NET application to create/start/stop Hyper-V virtual machine?

I'm looking to create an automated means of starting/stopping virtual machines from a C# application. I know you can do it via P/Invoke but that's not the best option for me. Can anyone help me out here? Thanks!

Answer Wiki

Thanks. We'll let you know when a new response is added.

The best way is thru WMI. Here’s an except from one of my apps:

private void PerformActionOnVM(Task_VMChangeState Task)
{
 if (Task.DesiredState == Task_VMChangeState.DesiredStateEnum.Create_Snapshot)
 {
  CreateSnapshot(Task); // (out of scope of this snippet)
  return;
 }

 ManagementObject VM = null;
 SelectQuery oQuer = new SelectQuery(string.Format(“SELECT * FROM MSVM_ComputerSystem WHERE ” +
  “Caption = ‘Virtual Machine’ and ElementName = ‘{0}’”, Task.VMName.Replace(“‘”, “””)));

 ManagementScope myScop = new ManagementScope(@”\\” + Task.VMHostName + @”\root\virtualization”);
 ManagementObjectSearcher oResult = new ManagementObjectSearcher(myScop, oQuer);

 foreach (ManagementObject oIte in oResult.Get())
 {
  VM = oIte;
  break;
 }

 if (VM != null)
 {
  ObjectGetOptions Opts = new ObjectGetOptions(null, TimeSpan.MaxValue, true);
  ManagementPath MP = new ManagementPath(“Msvm_ComputerSystem”);
  //ManagementClass ServiceClass = new ManagementClass(myScop, MP, Opts);
  // uint32 RequestStateChange(
  // [in] uint16 RequestedState,
  // [out] CIM_ConcreteJob REF Job,
  // [in] datetime TimeoutPeriod
  
  object[] Args = new object[3];
  switch (Task.DesiredState)
  {
   //case Task_VMChangeState.DesiredStateEnum.Create_Snapshot: Arg2[0] = 3; break;
   //case Task_VMChangeState.DesiredStateEnum.Initiate_Guest_Shutdown: Arg2[0] = 3; break;
   case Task_VMChangeState.DesiredStateEnum.Save: Args[0] = 32769; break;
   case Task_VMChangeState.DesiredStateEnum.Start: Args[0] = 2; break;
   case Task_VMChangeState.DesiredStateEnum.Stop: Args[0] = 3; break;
  }

  Args[1] = null;
  Args[2] = null;
  object Result = VM.InvokeMethod(“RequestStateChange”, Args);
  AddLogEntry(“ChangeVMState; VM: {0}, desired state: {1}, result: {2}”, Task.VMName, Task.DesiredState, GetVMStateChangeResult(Result));
 }
 else
 {
  AddLogEntry(“ChangeVMState: VM not found: ” + Task.VMName);
 }
}

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following