VMware {code} Community
Ashwin89
Contributor
Contributor

Getting Moref using VM Name

Is there is any fastest way to get the moref of a VM using its name?Instead of traversing each VM and comparing its name.

Can we specify propertyfilter in any manner to achieve this?

0 Kudos
9 Replies
david_drew
Enthusiast
Enthusiast

This what you're looking for?

my $vm = Vim::find_entity_view( view_type => 'VirtualMachine', filter => {'name' => qr/$vmName/ } );

0 Kudos
Ashwin89
Contributor
Contributor

I am doing this is in C#.Following is the traversal spec in am specifying but where should I put the VM NAME?

TraversalSpec dCenterToHostTSpec = new TraversalSpec();

dCenterToHostTSpec.type = "Datacenter";

dCenterToHostTSpec.path = "vmFolder";

dCenterToHostTSpec.selectSet = new SelectionSpec[] { new SelectionSpec() };

dCenterToHostTSpec.selectSet[0].name = "traverseChild";

dCenterToHostTSpec.skip = true;

// Create the folder traversal.

TraversalSpec _folderToChild = new TraversalSpec();

_folderToChild.type = "Folder";

_folderToChild.path = "childEntity";

_folderToChild.name = "traverseChild";

_folderToChild.skip = false;

_folderToChild.selectSet = new SelectionSpec[] { new SelectionSpec(), dCenterToHostTSpec };

_folderToChild.selectSet[0].name = _folderToChild.name;

// Define which properties are required of the virtual machine. In this case

// return just the virtual machine name and uuid.

// Define the information we want to collect: the type of managed object, the property

// of that managed object. Set the all property

// to FALSE, indicating we do not want to collect all the properties of the managed object.

// We only want the property specified by the propSet property.

PropertySpec[] _propertySpecification = new PropertySpec[1];

_propertySpecification[0] = new PropertySpec();

_propertySpecification[0].all = false;

_propertySpecification[http://0].pathSet = new string[|http://0].pathSet = new string[] { "name" };

_propertySpecification[0].type = "VirtualMachine";

0 Kudos
Steve_Jin
Expert
Expert

Pls note the property collector does NOT do filtering for you. But you can always get all the possible VMs and filter one by yourself.

Steve JIN, VMware Engineering

Creator of VMware Infrastructure Java API: http://vijava.sf.net

VI Java API 2.0 --- 15 times faster than AXIS in loading, 4+ faster in deserialization; only 1/4 of the size required by AXIS. More importantly freedom to redistribute your applications.

Download, Samples, DocWiki, RSS Feed

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
0 Kudos
admin
Immortal
Immortal

There is no place holder to specify a Object name while fetching data using Traversal Spec, however once you can filter the data based on the name after you retrieve the data using RetrieveProperties API. Inacse you wanted to use Traversal Spec, then once you have ObjectContent array you can iterate through it at client-side to find the VM with specific name.

For example you have ocary as ObjectContent[] returned by RetrieveProperties, then you must filter out your Virtual Machine with vmname using following code snippet, here type is "VirtualMachine"

ObjectContent oc = null;

ManagedObjectReference mor = null;

DynamicProperty[] propary = null;

string propval = null;

bool found = false;

for (int oci = 0; oci < ocary.Length && !found; oci++) {

oc = ocary[oci|http://communities.vmware.com/community-document-picker.jspa?communityID=&subject=oci];

mor = oc.obj;

propary = oc.propSet;

if ((type == null) || (type != null && typeIsA(type, mor.type))) {

if (propary.Length > 0) {

propval = (string)propary[0].val;

}

found = propval != null && vmname.Equals(propval);

propval = null;

}

}

if (!found) {

mor = null;

}

return mor;

The second option to avoid such iterations is to use either of FindByInventoryPath or FindByIp or FindByUuid APIs.

0 Kudos
RickMeier
Contributor
Contributor

So each time I need to get the MoRef for a given VM, I need to download MoRef and Name of all VM's (possibly in the thousands) and scan through to find the one I need. Of course this gets multiplied by the frequency with which I need to find a given VM.

The only key I have for finding the VM is its name, I have no other way to constrain the search.

searchIndex.findVmByName where are you?

0 Kudos
LarryZio
Contributor
Contributor

Is this what you're needing? SearchIndex API has a FindByDnsName operation:

which finds the virtual machine by its DNS name specified using VMware Tools. SearchIndex.java and SearchIndex.cs in the SDK demonstrate using this API.

0 Kudos
RickMeier
Contributor
Contributor

Correct me if I'm wrong, but this interface looks up by DNS name which is not necessarily the VM's name. Also, it requires the VMware tools to be running. Often these two conditions would be such that looking up by DNS name or VM name are equivalent, but not always. What would be nice is to add a FindByVmName to the SearchIndex.

0 Kudos
LarryZio
Contributor
Contributor

I see what you mean. You're right, that would be nice.

0 Kudos
Onciliatory
Contributor
Contributor

0 Kudos