This procedure uses PropertyCollector to retrieve a property from a single VirtualMachine object with the RetrievePropertiesEx method, by obtaining a managed object reference to the parent object of the VirtualMachine in the inventory.
To retrieve a reference to the parent object of a specific virtual machine, you must:
- Prepare a PropertySpec to specify the parent property and the parentVApp property of the VirtualMachine class. A virtual machine can have only one parent, but the parent can be either a Folder object or a VirtualApp object. This example collects both properties, one of which will be null.
- Prepare an ObjectSpec to identify the starting object, which is the specified VirtualMachine. There is no need for a TraversalSpec because the property belongs to the starting object itself. However, the ObjectSpec.skip property is set to false; a value of true would cause the PropertyCollector not to collect properties from the starting object.
- Assemble a PropertyFilterSpec from the prepared data.
- Invoke the RetrievePropertiesEx method.
Prerequisites
- A virtual machine managed object reference in a variable named vmRef.
- An authenticated Web Services session with the vSphere server that manages the virtual machine.
- A VimPort binding provider referenced by the variable methods, which is attached to the vSphere server connection context.
- A PropertyCollector instance referenced by the variable pCollector.
Note: This procedure shows only how to use the
PropertyCollector. For a description of server connection and getting a reference to the
PropertyCollector, see
Build a Simple vSphere Client Application for the Web Services SDK.
Procedure
Results
The method returns a key-value pair, name and val, indicating the name of the parent property and a managed object reference to the parent object.
What to do next
DynamicProperty parent = getVMParentProperty( vmRef ); if (parent.getName().equals("parentVApp")) { System.out.format("VApp MOref: %s", parent.getVal()); } else { System.out.format("Folder MOref: %s", parent.getVal()); }