16 January 2012

Show Currently Connected vCenter Servers in PowerCLI 5.0.1

PowerCLI 5.0.1 was recently released and vNuggs decided to check it out. Almost immediately we noticed something was missing. PowerCLI was no longer reporting in the title bar the vCenter server(s) to which we were connected.

Example of a single connected server before PowerCLI 5.0.1:


Example of multiple connected servers before PowerCLI 5.0.1:


AC decided to make a quick post to the PowerCLI community to see if others had noticed it and was hoping the PowerCLI team would respond as to whether this was intentional or not and what the future plans are for this functionality.  So far, no word from the team, but others have noticed the change as well.

However, in the meantime, we decided to solve the problem ourselves with some functions.  The first function is the one that does all the real work--ChangeTitleBar:

## Function to change title bar to reflect currently connected vCenters
## Author: vNugglets.com -- Jan 2012

function Change-TitleBar() {
    ## check to see if there are any currently connected servers
    if ($global:DefaultVIServers.Count -gt 0) {
        ## since there is at least one connected server, modify the window title variable accordingly
        $strWindowTitle = "[PowerCLI] Connected to {0} server{1}:  {2}" -f $global:DefaultVIServers.Count, $(if ($global:DefaultVIServers.Count -gt 1) {"s"}), (($global:DefaultVIServers | %{$_.Name}) -Join ", ")
    }
    else {
        ## since there are no connected servers, modify the window title variable to show "not connected"
        $strWindowTitle = "[PowerCLI] Not Connected"
    }
    ## perform the window title change
    $host.ui.RawUI.WindowTitle = $strWindowTitle
}

In line 6 the function merely checks the Count property of the array DefaultVIServers to ensure that you are connected to at least one server, then updates a variable with the new window title text in line 8. Note that we are omitting the name of the connected user.  We went this route to keep the function simple and because even VMware didn't include this info when connected to multiple servers (see screen shots above) and in the end, it is not something we find all that important/useful. It could simply be added by using the User property of the DefaultVIServers variable.

In line 12 (the "else" portion of the "if" statement) we just set the variable to what we selected as our default window title text.  Change this to your own preference (note that it doesn't bother reporting the PowerCLI version either, but that's easy enough with the Get-PowerCLIVersion cmdlet).

Finally, line 15 actually sets the window title to the defined value.  Now you just need to place this function in your PowerShell profile and make calls to it when you connect to/disconnect from VI servers.  You can do this by writing custom functions for Connect-VIServer and Disconnect-VIServer as follows:

## Modified "Connect- and Disconnect-VIServer" functions that call "Change-TitleBar" function
## Author: vNugglets.com -- Jan 2012

## Function for the "Connect-VIServer" cmdlet
function ConnectServer([string] $strVCenterName) {
    Connect-VIServer -Server $strVCenterName
    Change-TitleBar
}

## Function for the "Disconnect-VIServer" cmdlet
function DisconnectServer([string] $strVCenterName = "*") {
    Disconnect-VIServer -Server $strVCenterName -Confirm:$false
    Change-TitleBar
}

There is absolutely nothing fancy about these functions and they are not intended as direct feature-for-feature replacements of the actual Connect- and Disconnect-VIServer cmdlets.  The ConnVIServer function simply takes a string parameter and calls the actual Connect-VIServer cmdlet with that string specified as the server to connect to.  It then calls the Change-TitleBar function to do the title bar magic.  The DisconnVIServer function does the same, except in this case we've set a default parameter value of "*" just for convenience when you want to disconnect from all connected servers.  You'd merely type DisconnVIServer and it would immediately disconnect from all servers.

Lastly, it is probably a good idea to make a call to Change-TitleBar somewhere after the function definitions in your PowerShell profile.  Then it'll set the "default" title bar text as you launch PowerShell since the initial text with PowerCLI 5.0.1's initialization script just shows the name of the product and version.