PowerCLI is a great tool to manage your vSphere environment. No discussion about that!
The package contains cmdlets for, let’s say 80% of your vSphere day to day tasks, and for the missing 20% you can always fall back on the vSphere APIs (or ask in the PowerCLI community).
The only problem that pops up from time to time is the performance of some of the PowerCLI cmdlets.The Get-Vm cmdlet is one of these infamous cmdlets which, when executed in a decently sized (number of guests) vSphere environment, tends to be quite slow.
But as with the 80%-20% rule above, you can fall back on the vSphere APIs to speed things up a bit.
Add to that some medium to advanced PowerShell features and the time gains you can reach are impressive.
The problem at hand was that we needed a script to update a number of custom attributes on our guests.
A quick method of doing this would be something along these lines:
Quick and easy, but you’re in for a surprise when you run this in your 1000+ guest environment. You could have considerably more than 1 coffee while this is running.
A faster alternative is to use the Get-View cmdlet with the -ViewType VirtualMachine parameter. This cmdlet gets the objects that represent guests directly from the vSphere environment without converting them to PowerCLI objects.
The only problem we have now is that the Get-View cmdlet returns a VirtualMachine (vSPhere) object instead of a VirtualMachineImpl (PowerCLI) object, like the Get-Vm cmdlet does.
Luckily there is a PowerCLI cmdlet that allows us to go from a VirtualMachine object to a VirtualMachineImpl object; the Get-VIObjByVIView cmdlet.
Our script becomes
This will run a bit faster, but not that fast that it would bring down your coffee addiction.
As always, when at a dead-end with PowerCLI look in the APIs.
We find a method called setCustomValue on the ExtensibleManagedObject object.The ExtensibleManagedObject object is extended by the ManagedEntity object and that in it's turn is extended by the VirtualMachine object.
And that's the kind of object we had from our Get-View -ViewType VirtualMachine cmdlet. Looks promising.
But wait a minute, the setCustomValue method requires 2 parameters, key and value.The value parameter is easy, that is a string that contains the new content of the custom attribute.But how do we get at the key parameter ?
For most vSphere objects there is a manager available that provides additional methods and properties for the specific types of objects. The same goes for the custom fields.
Each CustomFieldDef object defines the name and key of a custom attribute. So this looks the place where we find the link between the custom attribute name and the key.
- Let's store this information in a kind of dictionary (in PS lingo called a hash table).
- one where we can look up a key when we know the name of the custom attribute
- one where we can find the name when we know the key of the custom attribute
We construct two hash tables
Now we need to get our VirtualMachine objects. The fastest way of getting these is with the Get-View -ViewType VirtualMachine cmdlet.
Now we need to consider how we are going to store these objects. For that we better first list what we actually want to do with these objects. In our case we came up with the following requirements:
- store the VirtualMachine object to be able to use the setCustomValue method
- store the actual contents of the custom attributes for each guest
- create a report that lists all guests and their custom attributes
- import/export the custom attributes from/to a CSV file
- find all guests that have a specific value in a custom attribute
- update a specific custom attribute for specific guests
After some trial and error I came up with this structure
The outer structure is a hash list with the guest’s name as the key.
The value part of this outer structure is a custom object with two properties:
- Custom: is another hash table which contains all custom attributes for that guest
- Object: is the actual VirtualMachine object
Based on this structure we can now script all the requirements.
- 1) Fill the hash table with all the guests and all their custom attributes for which there is a value.
- 2) Report all guests and their custom fields
- 3) Export all custom attributes to a CSV file
- 4) Import all custom attributes from a CSV file
- 5) Add/update a custom field
- 6) Find all guests that have a specific custom attribute set to a specific value
- 7) Update a specific custom field
Now that we have all the functionality we need it’s time to see if we were able to lower the execution time.In my limited test environment I was able to execute the custom attribute updates in 1/40th of the original time.
For a test in a bigger environment I got the help from Arnim van Lieshout (thanks Arnim).
He was able to bring the execution time of one custom attribute update on all the guests in his VI environment down from 70 seconds to less than 1 second.
Arnim’s environment contained 842 guests.
It would mean that he could do his planned updates of the custom attributes on all the guests in less than 1 hour, where he previously would have needed more than 65 hours.
In the mean time I received confirmation from Carter Shanklin that there is indeed a bug in the current release of the Set-CustomField cmdlet that could explain part of this behaviour.
My conclusion, it is sometimes useful to dive into the vSphere APIs and to learn the more advanced PowerShell techniques, like hash tables.