简体   繁体   中英

Get PoweredOn VM count in Vmware using VI Java

How to get the PoweredOn VM count in Vmware using the VI Java SDK? Is there a way to filter the VMs based on the power state?

The following code gives the VMs reference objects.

 ManagedEntity[] vms = new InventoryNavigator(clusterEntity).searchManagedEntities("VirtualMachine");

To filter the PoweredOn state VMs, we have to loop through the managed entities and retrieve the VM object, which is a time-consuming operation. Hence is there a way to get the count information alone?

One way of doing this is via the PropertyCollector 's RetrievePropertiesEx method.

Note that the PropertyCollector is a fairly complex beast - very briefly you need to provide it with a recursion strategy (eg recurse into child folders) as well as the properties you wish to extract. If you've never worked with the PropertyCollector before then you should look up a sample program online and modify it.

The nice thing about the PropertyCollector is that it is a single call that can retrieve a specific piece of information for a large number of entities. So it's quite efficient in that respect. You can also get the count pretty easily.

As you noted, the alternative is to query for each VM one by one, performing multiple queries and receiving much more data you have to process. Depending on the number of VMs you have/expect to have, and your performance requirements, you can select the best approach for you.

If you're on vSphere 6.5, then consider using the REST API. It has a list operation for VMs that accepts query parameters, and the power state is among the few properties that are exposed via that API. So you can get a list of powered on VMs by using the URL GET https://{server}/rest/vcenter/vm?filter.power_states.1=POWERED_ON . Note that this is limited to 1000 VMs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM