The hit's just keep coming, the two threads below were very helpful in attempting to connecting a clone to a specific port group. I don't kow if this will technically work, and at the moment it's not as asthetically pleasing as I'd like it to be, but I wanted to get this up before I head out.
http://communities.vmware.com/thread/267542
http://communities.vmware.com/message/1298627
string selectedVMname = cboClones.SelectedItem.ToString();
string selectedDatastore = cboDatastores.SelectedItem.ToString();
string SelectedDataCenter = cboDatacenters.SelectedItem.ToString();
string selectedPortGroup = cboPortGroups.SelectedItem.ToString();
string selectedCustomization = cboCustomizations.SelectedItem.ToString();
List<ManagedObjectReference> hostCollection = new List<ManagedObjectReference>();
Random rand = new Random();
ManagedObjectReference randomHost;
VirtualDevice networkDevice = new VirtualDevice();
sViServer = txtViServer.Text.ToString();
sUsername = txtUsername.Text.ToString();
sPassword = txtPassword.Text.ToString();
vimClient.Connect("https://" + this.sViServer + "/sdk");
vimSession = vimClient.Login(this.sUsername, this.sPassword);
vimServiceContent = vimClient.ServiceContent;
//
// Connect to datacenter
//
NameValueCollection dcFilter = new NameValueCollection();
dcFilter.Add("name", SelectedDataCenter);
EntityViewBase appDC = vimClient.FindEntityView(typeof(Datacenter), null, dcFilter, null);
Datacenter thisDC = (Datacenter)appDC;
//
// Get a list of hosts in the cluster
//
ManagedObjectReference hostFolderMoRef = thisDC.HostFolder;
Folder hostFolder = (Folder)vimClient.GetView(hostFolderMoRef, null);
ManagedObjectReference[] childEntityMoRefs = hostFolder.ChildEntity;
foreach (ManagedObjectReference childEntityMoRef in childEntityMoRefs)
{
ClusterComputeResource thisCluster = (ClusterComputeResource)vimClient.GetView(childEntityMoRef, null); ManagedObjectReference[] clusterHostMoRefs = thisCluster.Host; foreach (ManagedObjectReference clusterHostMoRef in clusterHostMoRefs) { HostSystem hostSystem = (HostSystem)vimClient.GetView(clusterHostMoRef, null); hostCollection.Add(hostSystem.MoRef); }
}
//
// Connect to selected vm to clone
//
NameValueCollection vmFilter = new NameValueCollection();
vmFilter.Add("Name", selectedVMname);
EntityViewBase thisVm = vimClient.FindEntityView(typeof(VirtualMachine), null, vmFilter, null);
VirtualMachine sourceVm = (VirtualMachine)thisVm;
//
// Randomly pick host from collection for folder
//
randomHost = hostCollection[rand.Next(0,hostCollection.Count)];
HostSystem selectedHost = (HostSystem)vimClient.GetView(randomHost, null);
//
// Connect to the datastore
//
NameValueCollection dsFilter = new NameValueCollection();
dsFilter.Add("name", selectedDatastore);
EntityViewBase myDs = vimClient.FindEntityView(typeof(Datastore), null, dsFilter, null);
Datastore selectedStore = (Datastore)myDs;
//
// Connect to portgroup
//
NameValueCollection pgFilter = new NameValueCollection();
pgFilter.Add("name", selectedPortGroup);
EntityViewBase myPg = vimClient.FindEntityView(typeof(DistributedVirtualPortgroup), null, pgFilter, null);
DistributedVirtualPortgroup selectedPg = (DistributedVirtualPortgroup)myPg;
//
// Connect to the customizationspec
//
CustomizationSpecManager specManager = (CustomizationSpecManager)vimClient.GetView(vimServiceContent.CustomizationSpecManager, null);
CustomizationSpecItem thisSpec = specManager.GetCustomizationSpec(selectedCustomization);
SeSparseVirtualDiskSpec diskSpec = new SeSparseVirtualDiskSpec();
diskSpec.DiskType = "thin";
VirtualMachineCloneSpec mySpec = new VirtualMachineCloneSpec();
mySpec.Location = new VirtualMachineRelocateSpec();
mySpec.Location.Datastore = selectedStore.MoRef;
mySpec.Location.Transform = VirtualMachineRelocateTransformation.sparse;
mySpec.Location.Host = randomHost;
mySpec.Customization = thisSpec.Spec;
mySpec.Config = new VirtualMachineConfigSpec();
// get nic on vm
foreach (VirtualDevice vDevice in sourceVm.Config.Hardware.Device)
{
if (vDevice.DeviceInfo.Label.Contains("network")) { networkDevice = vDevice; }
}
VirtualDeviceConfigSpec devSpec = new VirtualDeviceConfigSpec();
devSpec.Device = networkDevice;
VirtualEthernetCardNetworkBackingInfo nicBack = new VirtualEthernetCardNetworkBackingInfo();
nicBack.DeviceName = cboPortGroups.SelectedItem.Text;
nicBack.Network = selectedPg.MoRef;
devSpec.FileOperation = VirtualDeviceConfigSpecFileOperation.replace;
devSpec.Device.Backing = nicBack;
mySpec.Config.DeviceChange[0] = devSpec;
//
// Perform the clone
//
//sourceVm.CloneVM_Task(sourceVm.Parent, "my-sample-vm", mySpec);
vimClient.Disconnect();