Java Example of Filtering Virtual Machines
The code example is based on the VmHelper.java sample file.
The following code example shows how you can retrieve the VM ID of a virtual machine with a specific name.
This example uses the steps that are described in Filtering Virtual Machines.
Note: For a complete and
up-to-date version of the sample code, see the
vSphere
Automation SDK Java samples at GitHub.
... public static String getVM( StubFactory stubFactory, StubConfiguration sessionStubConfig, String vmName) { VM vmService = stubFactory.createStub(VM.class, sessionStubConfig); // Get summary information about the virtual machine VMTypes.FilterSpec vmFilterSpec = new VMTypes.FilterSpec.Builder() .setNames(Collections.singleton(vmName)).build(); List<VMTypes.Summary> vmList = vmService.list(vmFilterSpec); assert vmList.size() > 0 && vmList.get(0).getName().equals( vmName) : "VM with name " + vmName + " not found"; return vmList.get(0).getVm(); } ...