TA2650 scripts – Part 5 – Proxy cmdlets – customise the PowerCLI cmdlets

Carter Shanklin and his development team are doing a wonderful job with PowerCLI. The number of available cmdlets keeps growing with each new build they publish.

But sometimes one of the PowerCLI cmdlets is missing a parameter you would like to use or has a parameter you don’t want to use in your environment. With the advent of PowerShell v2 you can now customise the PowerCLI cmdlets to your liking with the help of proxy cmdlets.

The first time I heard of “proxy cmdlets” was in Windows PowerShell blog entry called  Extending and/or Modifying Commands with Proxies from the PS architect himself, Jeffrey Snover.

To enable us mere mortals to use this feature Jeffrey Snover provided a module called MetaProgramming in his blog entry. With the help of the function, called New-ProxyCommand, we can now easily emit a script cmdlet from any of the PowerCLI cmdlets.

As an example I have taken the Get-StatType cmdlet. This cmdlet will show for any vSphere entity what statistical metrics are available.

The problem is that the cmdlet only shows the metric but not the instances. So it might look as if some of the metrics are repeated.

In the above screenshot you notice that some of the cpu-related metrics appear twice in the list. That’s because there are 2 instances for each of these metrics; the aggregate instance, which is the average of all other instances, and an instance per CPU present on the host (in this case 1 CPU).

Additionally, sometimes it might be useful for the PowerCLI user to actually see which instances are available for a specific metric.

Following the instructions in the blog entry we use the New-ProxyCommand cmdlet to generate a PS1 file that will add a new parameter and will call the Get-StatType cmdlet.

The ps1 file that comes out is a skeleton proxy cmdlet. The file contains a lot of help and one only needs to add the additional coding in the proper cmdlet block.

Since this post only wanted to show how to use proxy cmdlets with PowerCLI cmdlets, I will not go into the details of the script.

For closers this is what the “new” Get-StatType cmdlet will do.

metaprog-2

Each metric is displayed only once and the line contains a new property, called Instances, which lists all available instances for the metric. The aggregate instance is represented by an empty string (“”).

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Buy the Book