/// <summary> /// Register the virtual machine. /// </summary> /// <returns>Returns the boolean value true or false.</returns> private Boolean registerVirtualMachine() { Boolean registered = false; Console.WriteLine("Registering The Virtual Machine .........."); // Get datacenter var serviceUtil = cb.getServiceUtil(); var dataCenterMoref = serviceUtil.GetDecendentMoRef(_sic.rootFolder, "Datacenter", getDataCenter()); var vmFolderMoref = (ManagedObjectReference)serviceUtil.GetDynamicProperty(dataCenterMoref, "vmFolder"); // Get datastore var datastoreMorefs = (ManagedObjectReference[])serviceUtil.GetDynamicProperty(dataCenterMoref, "datastore"); var dirSize = getDirSize(getLocalPath()); ManagedObjectReference targetDatastoreMoref = null; foreach (var datastoreMoref in datastoreMorefs) { var datastoreSummary = (DatastoreSummary)serviceUtil.GetDynamicProperty(datastoreMoref, "summary"); if (datastoreSummary.name.Equals(getDataStore(), StringComparison.CurrentCultureIgnoreCase)) { var datastoreInfo = (DatastoreInfo)serviceUtil.GetDynamicProperty(datastoreMoref, "info"); if (datastoreInfo.freeSpace > dirSize) { targetDatastoreMoref = datastoreMoref; break; } } } if (targetDatastoreMoref == null) { Console.WriteLine("Could not find user entered datastore."); return(registered); } // Select the first accessible host attached to the datastore var datastoreHostMounts = (DatastoreHostMount[])serviceUtil.GetDynamicProperty(targetDatastoreMoref, "host"); ManagedObjectReference targetHostSystemMoref = null; foreach (var datastoreHostMount in datastoreHostMounts) { if (datastoreHostMount.mountInfo.accessible) { targetHostSystemMoref = datastoreHostMount.key; } } if (targetHostSystemMoref == null) { Console.WriteLine( "No accessible host found in datacenter that has the specified datastore and free space."); return(registered); } else { // Get vmx path var vmxPath = "[" + getDataStore() + "] " + getVmName() + "/" + getVmName() + ".vmx"; // The parent of a host system is either ComputeResource or ClusterComputeResource var computeResourceRef = (ManagedObjectReference)serviceUtil.GetDynamicProperty(targetHostSystemMoref, "parent"); // Get resource pool of the compute resource var resourcePoolMoref = (ManagedObjectReference)serviceUtil.GetMoRefProp(computeResourceRef, "resourcePool"); // Registering the virtual machine var taskmor = _service.RegisterVM_Task( vmFolderMoref, vmxPath, getVmName(), false, resourcePoolMoref, targetHostSystemMoref); string result = serviceUtil.WaitForTask(taskmor); if (result.Equals("sucess")) { Console.WriteLine("Registering The Virtual Machine ..........Done"); registered = true; } else { Console.WriteLine("Some Exception While Registering The VM"); registered = false; } return(registered); } }