So here is a fun one that I have been debugging for a couple of hours and I hope someone can shed some light on it.
Scenario:
ESXi 4.1 server - Two virtual machines on the server
VM1 = WIN2K8-32
VM2 = WIN2K8-32-1
Code: Pass in "WIN2K8-32" as either the "name" or "Config.Name" parameter in the filter.
// Create filter by VM name
NameValueCollection nvcFilter = new NameValueCollection();
//nvcFilter.Add("name", "WIN2K8-32");
nvcFilter.Add("Config.Name", "WIN2K8-32");
// Get VirtualMachine view object
VirtualMachine oVirtualMachine = (VirtualMachine)m_oVimClient.FindEntityView(typeof(VirtualMachine), null, nvcFilter, null);
The returned VirtualMachine object is actually the object for VM2. I do not have a ^ or any other wildcard denoting character in my filter keywords. Does anyone know why it is doing this and more importantly how to stop it?
Thanks,
Jim
The code below is how I worked around the defect, but it is a significant one and I hope VMware decides to fix it.
// Create filter by VM name
NameValueCollection nvcFilter = new NameValueCollection();
nvcFilter.Add("name", sVirtualMachine);
// Get a complete list of VM's that match the criteria. Due to a defect in the "FindEntityView" call that returns the "last of"
// record that partially matches the string (for example, two machines "TEST" and "TEST1". Using a filter of name "TEST" will
// return a virtual machine object for "TEST1" even when no wildcard characters (^) are used. See VMware community post
// http://communities.vmware.com/post!reply.jspa?thread=322898 for more details.
IList<EntityViewBase> vmList = m_oVimClient.FindEntityViews(typeof(VirtualMachine), null, nvcFilter, null);
VirtualMachine oVirtualMachine = null;
// Iterate through all of the VMs returned in the list and match the exact string we are looking for, working around the defect
// described above.
foreach (VirtualMachine vm in vmList)
{
if (vm.Config.Name.Equals(sVirtualMachine, StringComparison.CurrentCultureIgnoreCase))
{
oVirtualMachine = vm;
break;
}
}
VMware, please fix the API