dsj032
Contributor
Contributor

List vm info given vm name

How can i get the vm information(details like os, memory usage etc.) having only datacenter name and vm name. I dont have vmdns name.

0 Kudos
2 Replies
Steve_Jin
Expert
Expert

Having only datacenter name and vm name might not be enough to get "the" VM since you can have more than one VM named the same in a datacenter.

If you use VI Java API (), you can get hold of the VM as follows:

Datacenter dc = (Datacenter) new InventoryNavigator(rootFolder).searchManagedEntity("Datacenter", dcName);

VirtualMachine vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmName);

String osName = vm.getGuest().getGuestFullName();

...

Steve JIN, VMware Engineering

Creator of VI Java API:

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

You need to traverse the inventory for Virtual Machine object and filter the Virtual machine using its name. Please refer following code snippet:

//here dcMoRef is Datacenter MoRef and name is Virtual machine name

string name;

string datacenterName;

ManagedObjectReference dcMoRef

= service.FindByInventoryPath(sic.searchIndex, datacenterName);

PropertySpec pSpec = new PropertySpec();

pSpec.type="VirtualMachine";

pSpec.pathSet=new String[] { "name","runtime.powerState" };

SelectionSpec recurseFolders = new SelectionSpec();

recurseFolders.name="folder2childEntity";

TraversalSpec folder2childEntity = new TraversalSpec();

folder2childEntity.type="Folder";

folder2childEntity.path="childEntity";

folder2childEntity.name=recurseFolders.name;

folder2childEntity.selectSet=

new SelectionSpec[] ;

// Traverse from a Datacenter through the 'vmFolder' property

TraversalSpec dc2vmFolder = new TraversalSpec();

dc2vmFolder.type="Datacenter";

dc2vmFolder.path="vmFolder";

dc2vmFolder.selectSet=

new SelectionSpec[] ;

ObjectSpec oSpec = new ObjectSpec();

oSpec.obj=dcMoRef;

oSpec.skip=true;

oSpec.selectSet=new SelectionSpec[] ;

PropertyFilterSpec pfSpec = new PropertyFilterSpec();

pfSpec.propSet=new PropertySpec[] ;

pfSpec.objectSet=new ObjectSpec[] ;

ObjectContent[] ocary = service.RetrieveProperties(sic.propertyCollector,

new PropertyFilterSpec[] );

iObjectContent 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];

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 && name.Equals(propval);

propval = null;

}

}

if (!found) {

mor = null;

}

0 Kudos