Proxy cmdlet revisited: Connect-VIServer and Disconnect-VIServer

In PowerCLI 5.0.1 a handy feature that showed the connected vSphere Servers in the title bar of the PowerCLI window was apparently removed.
In a PowerCLI Community thread some users found this a useful feature that they would like to have back.
I’m sure the PowerCLI Team will listen to their users and fix this problem in the coming PowerCLI version.

But while we are waiting for a new PowerCLI build that brings back the title bar text, you can fix this for yourself thanks to the proxy cmdlet feature.

Proxy cmdlets is (another) handy feature in PowerShell which allows you to modify and extend existing cmdlets. You can add parameters, change the output and do other crazy things. In the Extending and/or Modifing Commands with Proxies post from Jeffrey Snover you can find all the details.

In that same post you can also find the MetaProgramming module which makes it a breeze to customise cmdlets.

You just call the New-ProxyCommand and redirect the output to a .ps1 file, which you can then use to write your customisations for the cmdlet.

In this case I did that for the Connect-VIServer and Disconnect-VIServer cmdlets as follows

The resulting .ps1 files can now be edited to introduce our customisations to these cmdlets.

My Connect-VIServer

Annotations

Line 1,107: We make this a function
Line 78,90: The code we add to the original cmdlet. This will place the text in the title bar.

My Disconnect-VIServer

Annotations

Line 1,65: We make this a function
Line 36,48: The code we add to the original cmdlet. This will place the text in the title bar.

Usage samples

First you will need to dot-source the .ps1 files with the customised versions of both cmdlets.

We now have a function and a cmdlet for both.

But PowerShell will use a function before a cmdlet when both have the same name. Exactly what we need !

If you want to learn more about these precedences, do a

Now we are ready to use our customised cmdlets. When we do a first connect

the PowerCLI window will show

When we connect an additional vSphere Server

the PowerCLI window will show.

When we disconnect from one of the vSphere Servers, the title bar will be updated as well.

The title bar will show

There are of a couple of open problems

  • The initial PowerCLI window shows “Not connected“. This could eventually be solved by adapting the PowerCLI initialisation script, but I didn’t want to change that script
  • The Disconnect-VIServer cmdlet also accepts a [string] for the Server argument. The proxy cmdlet doesn’t accept that, you will have to give it a VIServer object on the Server parameter.

Enjoy !

Leave a Reply

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

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.