protected override void DoExecute(ITaskContext context) { if (string.IsNullOrEmpty(hostName)) { throw new TaskExecutionException("Host name can not be empty!"); } if (string.IsNullOrEmpty(machineName)) { throw new TaskExecutionException("Machine name can not be empty!"); } if (string.IsNullOrEmpty(machineLocation)) { throw new TaskExecutionException("Machine location can not be empty!"); } using (HyperVManager manager = new HyperVManager()) { manager.Connect(hostName); if (string.IsNullOrEmpty(diskPath)) { diskPath = Path.Combine(machineLocation, machineName + ".vhd"); IVirtualTask disk = manager.CreateDynamicDisk(diskPath, 50); disk.WaitForCompletion(new TimeSpan(0, 0, 0, 10)); } IVirtualTask t = manager.CreateVirtualMachine( machineName, machineLocation, diskPath, networkAdapterName, mac, memorySize); t.WaitForCompletion(new TimeSpan(0, 0, 1, 0)); } }
protected override void DoExecute(ITaskContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (string.IsNullOrEmpty(hostName)) { throw new TaskExecutionException("Host name can not be empty!"); } if (string.IsNullOrEmpty(machineName)) { throw new TaskExecutionException("Machine name can not be empty!"); } try { using (HyperVManager manager = new HyperVManager()) { manager.Connect(hostName); IVirtualTask t = manager.DeleteVirtualMachine(machineName); t.WaitForCompletion(new TimeSpan(0, 0, 0, 10)); } } catch (Exception e) { if (failIfNotExists) { throw new TaskExecutionException("Error deleting virtual machine", e); } context.WriteDebug("Virtual machine does not exist and was not deleted!"); } }
protected override void DoExecute(ITaskContext context) { using (HyperVManager manager = new HyperVManager()) { manager.Connect(hostName); IVirtualTask t; switch (type) { case CreateDiskType.Fixed: if (string.IsNullOrEmpty(imagePath) || size < 1) { throw new ArgumentException("ImagePath or size not set"); } t = manager.CreateFixedDisk(imagePath, size); break; case CreateDiskType.Dynamic: if (string.IsNullOrEmpty(imagePath) || size < 1) { throw new ArgumentException("ImagePath or size not set"); } t = manager.CreateDynamicDisk(imagePath, size); break; case CreateDiskType.Differencing: if (string.IsNullOrEmpty(imagePath) || string.IsNullOrEmpty(baseImagePath)) { throw new ArgumentException("ImagePath or BaseImagePath not set"); } t = manager.CreateDifferencingDisk(imagePath, baseImagePath); break; default: throw new NotSupportedException("Not supported disk type."); } t.WaitForCompletion(new TimeSpan(0, 0, 1, 0)); } }
protected override void DoExecute(ITaskContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (string.IsNullOrEmpty(hostName)) { throw new TaskExecutionException("Host name can not be empty!"); } if (string.IsNullOrEmpty(machineName)) { throw new TaskExecutionException("Machine name can not be empty!"); } using (HyperVManager manager = new HyperVManager()) { manager.Connect(hostName); IVirtualTask t = manager.StartVirtualMachine(machineName); context.WriteDebug("Waiting for machine to start..."); t.WaitForCompletion(new TimeSpan(0, 0, 2, 0)); } }
protected override void DoExecute(ITaskContext context) { if (string.IsNullOrEmpty(machineName)) { throw new TaskExecutionException("Machine name must be specified!"); } if (string.IsNullOrEmpty(machineLocation)) { throw new TaskExecutionException("Machine location must be specified!"); } if (string.IsNullOrEmpty(baseMachineName)) { throw new TaskExecutionException("Base machine name must be specified!"); } using (HyperVManager manager = new HyperVManager()) { manager.Connect(hostName); IVirtualTask t = manager.CloneVirtualMachine( machineName, machineLocation, baseMachineName, diskPath, networkAdapterName, mac); t.WaitForCompletion(new TimeSpan(0, 0, 1, 0)); } }