PowerCLI 5.0.1 Connect-VIServer repaired
January 16, 2012 Leave a comment
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.