PowerCLI 5.0.1 Connect-VIServer repaired

PowerCLI logo
PowerCLI up to version 5.0.1 showed the connected servers in the title bar of the window. I found this very handy. Unfortunately though VMware has removed this feature in version 5.0.1. In this blogpost I present you a script that returns this feature.

PowerShell proxy functions

In PowerShell it is possible to make a wrapper function around a cmdlet, that extends the functionality of the cmdlet. This is called a proxy function. There is a very good example on the “Hey, Scripting Guy! Blog”:
Proxy Functions: Spice Up Your PowerShell Core Cmdlets.

I used this example to create proxy functions for the PowerCLI Connect-VIServer and Disconnect-VIServer cmdlets, that return the pre version 5.0.1 behaviour.

The PowerCLI script in listing 1 gives you Connect-VIserver and Disconnect-VIServer proxy functions to repair the pre version 5.0.1 windows title. You can add these functions to your PowerShell profile. Or you can put them in a .ps1 file and dot-source this file to add the functions to your PowerCLI session.

For more information on editing your PowerShell profile or dot-sourcing a script see listing 11 and 8 from my previous blog post: Speed up your PowerCLI cmdlet typing with aliases.

function Connect-VIServer {
[CmdletBinding(DefaultParameterSetName='Default')]
param(
    [Parameter(ParameterSetName='Default', Mandatory=$true, Position=0)]
    [ValidateNotNullOrEmpty()]
    [System.String[]]
    ${Server},

    [Parameter(ParameterSetName='Default')]
    [ValidateRange(0, 65535)]
    [ValidateNotNull()]
    [System.Int32]
    ${Port},

    [Parameter(ParameterSetName='Default')]
    [ValidateSet('http','https')]
    [System.String]
    ${Protocol},

    [Parameter(ParameterSetName='Default', ValueFromPipeline=$true)]
    [ValidateNotNullOrEmpty()]
    [System.Management.Automation.PSCredential]
    ${Credential},

    [Parameter(ParameterSetName='Default', ValueFromPipeline=$true)]
    [Alias('Username')]
    [System.String]
    ${User},

    [Parameter(ParameterSetName='Default')]
    [System.String]
    ${Password},

    [Parameter(ParameterSetName='Default')]
    [System.String]
    ${Session},

    [Parameter(ParameterSetName='Default')]
    [Switch]
    ${NotDefault},

    [Parameter(ParameterSetName='Default')]
    [Switch]
    ${SaveCredentials},

    [Parameter(ParameterSetName='Default')]
    [Switch]
    ${AllLinked},

    [Parameter(ParameterSetName='Menu', Mandatory=$true)]
    [Switch]
    ${Menu})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Connect-VIServer', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
		$PowerCLIVersion = Get-PowerCLIVersion
		$PowerCLIVersionString = "VMware vSphere PowerCLI $($PowerCLIVersion.Major).$($PowerCLIVersion.Minor).$($PowerCLIVersion.Revision)"
		if (-not (Get-Variable DefaultVIServers -ErrorAction:SilentlyContinue) -or $DefaultVIServers.Length -eq 0) {
		  $WindowTitle = "$PowerCLIVersionString - Not Connected"
		}
		elseif ($DefaultVIServers.Length -eq 1) {
		  $WindowTitle = "$PowerCLIVersionString - Connected to $($DefaultVIServers[0].Name) as $($DefaultVIServers[0].User)"
		}
		else {
		  $WindowTitle = "$PowerCLIVersionString - Connected to $($DefaultVIServers.Length) servers: "+[string]::Join(',',$DefaultVIServers)
		}
		(Get-Host).UI.RawUI.WindowTitle = $WindowTitle
    } catch {
        throw
    }
}
<#

.ForwardHelpTargetName Connect-VIServer
.ForwardHelpCategory Cmdlet

#>
}

function Disconnect-VIServer {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
param(
    [Parameter(Position=0, ValueFromPipeline=$true)]
    [ValidateNotNullOrEmpty()]
    [PSObject[]]
    ${Server},

    [Switch]
    ${Force})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Disconnect-VIServer', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
		$PowerCLIVersion = Get-PowerCLIVersion
		$PowerCLIVersionString = "VMware vSphere PowerCLI $($PowerCLIVersion.Major).$($PowerCLIVersion.Minor).$($PowerCLIVersion.Revision)"
		if (-not (Get-Variable DefaultVIServers -ErrorAction:SilentlyContinue) -or $DefaultVIServers.Length -eq 0) {
		  $WindowTitle = "$PowerCLIVersionString - Not Connected"
		}
		elseif ($DefaultVIServers.Length -eq 1) {
		  $WindowTitle = "$PowerCLIVersionString - Connected to $($DefaultVIServers[0].Name) as $($DefaultVIServers[0].User)"
		}
		else {
		  $WindowTitle = "$PowerCLIVersionString - Connected to $($DefaultVIServers.Length) servers: "+[string]::Join(',',$DefaultVIServers)
		}
		(Get-Host).UI.RawUI.WindowTitle = $WindowTitle
    } catch {
        throw
    }
}
<#

.ForwardHelpTargetName Disconnect-VIServer
.ForwardHelpCategory Cmdlet

#>
}

Listing 1. PowerCLI Connect-VIserver and DisconnectVIServer proxy functions to repair the pre version 5.0.1 windows title.

Annotations

Lines 084 – 095 and lines 149 – 160: are the lines I added to the functions that were generated by the code from the Hey, Scripting Guy! Blog post. I tried to stay as close as possible to the pre version 5.0.1 behaviour. But I added the v5.0.1 version number that I like.
Line 113: I modified this line to keep it possible to use strings as a parameter for the Disconnect-VIserver function.

Advertisement

About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 30 years of experience in the IT industry. He focusses on VMware vCloud Suite and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft PowerShell. Robert is the author of the books “Learning PowerCLI” and “Learning PowerCLI – Second Edition.” Robert is a frequent contributor and moderator at the VMware VMTN Communities. He has a bachelor degree in software engineering and holds the following IT certifications and accreditations: VSP 2016, VTSP 2016, VCP4-DCV, VCP5-DCV, VCP6-DCV, VCP6-CMA, VCA-Cloud, VCA-WM, VCA-NV, VMSP, VMTSP, ZCS, ZCP, ZCP-Cloud, MCSE, MCSA, MCP, MCP+I, PRINCE2 Foundation and ITIL Foundation. In 2012, 2013, 2014, 2015, 2016, 2017, 2018 and 2019 Robert received the VMware vExpert award for his contribution to the community of VMware users over the past year. In 2017 Robert also received the VMware vExpert Cloud award. PernixData made him in 2015 a member of the PernixPro.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: