public VirtualMachine CreateVM()
        {
            if (RegisteredVirtualMachineService.GetRegisteredVMImagePaths().Contains(VM.ImagePathName))
                throw new InvalidDataException("Specified VM path already exists");
            if (!VM.ImagePathName.StartsWith(AppConfiguration.GetDatastore()) || VM.BaseImageFullPhysicalPath.StartsWith(AppConfiguration.GetDatastore()))
                throw new InvalidDataException("Invalid ImagePathName: doesn't contain datastore name, or BaseImageFullPhysicalPath does contain datastore name");
            if (VM.ImagePathName.Length < 8 || VM.BaseImageFullPhysicalPath.Length < 8 || VM.IP.Length < 7)
                throw new InvalidDataException("CreateVM required field unspecified or too short");

            //this will run async, but probably should report status and handle errors in individual steps better
            try
            {
                CopyVMFiles(VM.BaseImageFullPhysicalPath, VM.ImagePathName);
            }
            catch (Exception ex)
            {
                throw new SchedulerInfo("Error copying files, VM creation aborted.", ex);
            }

            // Allot time to finish copying the file
            System.Threading.Thread.Sleep(16 * 1000);

            RegisteredVirtualMachineService service = null;
            try
            {
                RegisteredVirtualMachineService.GetVirtualHost().Register(VM.ImagePathName);
                service = new RegisteredVirtualMachineService(VM.ImagePathName);
                // Make triple-double-dog sure that the VM is online and ready.
                // Allow VM time to power on
                service.PowerOn();
                System.Threading.Thread.Sleep(180 * 1000);

                // Allow VM time to reboot
                service.Reboot();
                System.Threading.Thread.Sleep(250 * 1000);
            }
            catch (Exception ex)
            {
                new SchedulerInfo("Error registering or first-booting new VM, will attempt to continue", ex).LogElmah();
            }
            SetIPHostname(service);

            // Allow VM time to reboot
            service.Reboot();
            System.Threading.Thread.Sleep(250 * 1000);

            if (!VM.IsAutoStarted)
                service.PowerOff();

            return VM;

            //http://vmwaretasks.codeplex.com/discussions/276715
            //"[ha-datacenter/standard] Windows Server 2003/Windows Server 2003.vmx"
            //http://communities.vmware.com/message/1688542#1688542
            //http://panoskrt.wordpress.com/2009/01/20/clone-virtual-machine-on-vmware-server-20/
            //we don't seem to have vmware-vdiskmanager
        }
 public void PowerOnTest()
 {
     VirtualMachineRepository vmr = new VirtualMachineRepository();
     VirtualMachine vm = vmr.GetAllRegisteredVirtualMachines().GetEnumerator().Current; // TODO: Initialize to an appropriate value
     RegisteredVirtualMachineService target = new RegisteredVirtualMachineService(vm); // TODO: Initialize to an appropriate value
     if (target.GetStatus() == VirtualMachine.STOPPED)
     {
         target.PowerOn();
         Assert.IsTrue(target.GetStatus() == VirtualMachine.POWERINGON || target.GetStatus() == VirtualMachine.RUNNING);
     }
     else
         Assert.Inconclusive();
 }
 private void PowerOn(VirtualMachine vm, RegisteredVirtualMachineService service)
 {
     vm.Status = VirtualMachine.POWERINGON;
     dataDB.SaveChanges();
     service.PowerOn();
     vm.Status = VirtualMachine.RUNNING;
     vm.LastStarted = DateTime.Now;
     dataDB.SaveChanges();
 }