The vCenter plugin has a very useful method for retrieving a list of objects quickly and easily. However, if your inventory is quite large, then this can be quite slow. There are two ways that this can be improved. 1) only retrieve the attributes that are useful for our code requirements, 2) use an XPATH query filter to limit the scope of the search.
In my situation there were over a 100 Datastores but I only wanted to retrieve a list based on a given prefix (of which there were less than 10). The Datastore names end with an incrementing number and I wanted to return all of these in an array.
// Set the Datastore name Prefix to search on. var DsNamePrefix = "DS_MYDATASTORE_"; // Declare which Datastore properties we care about to speed up processing. var DsProps = ['info', 'summary']; // Setting the XPATH massively improves performance retrieving the Datastore list. var XPath = "xpath:@name[starts-with(.,'" + DsNamePrefix + "')]"; // Get an array of all the datastores found. var VcDsArray = VcPlugin.getAllDatastores(DsProps, XPath); System.debug("Number of datastores found: " + VcDsArray.length);
I have used a similar approach which allowed me to retrieve several thousand virtual machines in approximately 3-5 seconds.