PowerCLI GUI for VM Consoles

A few months ago, Dimitar Barfonchovski created a blog post on accessing the VM Console via PowerCLI, hosted on the PowerCLI Blog. Over the next few days, some cool enhancements came out focusing on a GUI for the featureset – which appear to have been lost to the great bitbucket in the sky, or I’d give some credit to them (if you know what I’m talking about, drop a link in the comments and I’ll update the article). I added a few enhancements of my own and ended up with a quick a dirty pastebin that would allow anyone to authenticate to any vCenter server and get a list of VMs they have access to.

I finally went back and combined this with last Friday’s post on creating a PowerCLI module. With a few tweaks we end up with an auto-import module and the cmdlet Get-VMConsoles. Any user can run this cmdlet. Upon connection to the specified vCenter server, you are prompted for authentication. After successfully authenticating, click Open VM Consoles and you are presented with a list of consoles available to your user. You can add a few filters, as I have done below. Ctrl-click to select the VMs you want to view and hit OK in the bottom right. Your consoles will open in your system’s browser in separate tabs.

As mentioned above, this is quick and dirty. It was built with PrimalForms Community Edition to get the GUI going and then I massaged it into a module. It could use some enhancements but should work fine. One note, the window it creates, titled VM Console Viewer, sometimes pops under your ISE window. If you launch it and don’t see anything, check the taskbar, it’s probably there. I welcome any updates or fixes in the comments.

Enjoy!

Get-VMConsoles Output

Note: The function below can be integrated to an existing module, or a new, single cmdlet module can be created.

UPDATE: I’ve added this to my GitHub repo. You can download it directly from there.

<#
.Synopsis
   Get a list of VM Consoles and display them graphically.
.DESCRIPTION
   A graphical utility for connecting to a vCenter server and connecting to available HTML5 consoles
.EXAMPLE
   Get-VMConsoles vcenter.contoso.com
.EXAMPLE
   Another example of how to use this cmdlet
#>
function Get-VMConsoles
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
    )

    Begin
    {
    }
    Process
    {
        $defaultvCenter = "vcenter"
        $viServer = $null

        #Generated Form Function
        function GenerateForm {
        ########################################################################
        # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
        # Generated On: 10/24/2013 11:00 AM
        ########################################################################

        #region Import the Assemblies
        [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
        [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
        #endregion

        #region Generated Form Objects
        $form1 = New-Object System.Windows.Forms.Form
        $hostnameTextbox = New-Object System.Windows.Forms.TextBox
        $connectButton = New-Object System.Windows.Forms.Button
        $exitButton = New-Object System.Windows.Forms.Button
        $openButton = New-Object System.Windows.Forms.Button
        $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
        #endregion Generated Form Objects

        #----------------------------------------------
        #Generated Event Script Blocks
        #----------------------------------------------
        #Provide Custom Code for events specified in PrimalForms.
        $exitButton_OnClick= 
        {
            if ($global:viServer) {
                $global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
            }
            $form1.Close()
        }

        $connectButton_OnClick= 
        {
            $vCenter = $hostnameTextbox.Text
            $global:viServer.IsConnected

            if ($viServer.IsConnected) {
                Write-Host "Disconnecting from $vCenter"
                $global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
                $openButton.Enabled = $false
            }
            $global:viServer = Connect-VIServer $vCenter -ErrorAction SilentlyContinue
            if ($global:viServer.IsConnected) {
                Write-Host "Successfully connected to $vCenter"
                $openButton.Enabled = $true
            }
            else {
                [System.Windows.Forms.MessageBox]::Show("Failed to connect to vCenter server '$vCenter'", "Connection Failed")
                $global:viServer = Disconnect-VIServer -server $global:viServer.name -Force -Confirm:$false
                $openButton.Enabled = $false
            }
        }

        $openButton_OnClick= 
        {
            $Selection = Get-VM | Out-GridView -OutputMode Multiple
            $Selection | Foreach { Open-VMConsoleWindow $_ }
        }

        $OnLoadForm_StateCorrection=
        {#Correct the initial state of the form to prevent the .Net maximized form issue
            $form1.WindowState = $InitialFormWindowState
        }

        #----------------------------------------------
        #region Generated Form Code
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Height = 108
        $System_Drawing_Size.Width = 284
        $form1.ClientSize = $System_Drawing_Size
        $form1.DataBindings.DefaultDataSourceUpdateMode = 0
        $form1.Name = "form1"
        $form1.Text = "VM Console Viewer"

        $hostnameTextbox.DataBindings.DefaultDataSourceUpdateMode = 0
        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 12
        $System_Drawing_Point.Y = 16
        $hostnameTextbox.Location = $System_Drawing_Point
        $hostnameTextbox.Name = "hostnameTextbox"
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Height = 20
        $System_Drawing_Size.Width = 156
        $hostnameTextbox.Size = $System_Drawing_Size
        $hostnameTextbox.TabIndex = 3
        $hostnameTextbox.Text = $defaultvCenter

        $form1.Controls.Add($hostnameTextbox)

        $connectButton.DataBindings.DefaultDataSourceUpdateMode = 0

        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 174
        $System_Drawing_Point.Y = 14
        $connectButton.Location = $System_Drawing_Point
        $connectButton.Name = "connectButton"
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Height = 23
        $System_Drawing_Size.Width = 98
        $connectButton.Size = $System_Drawing_Size
        $connectButton.TabIndex = 2
        $connectButton.Text = "Connect"
        $connectButton.UseVisualStyleBackColor = $True
        $connectButton.add_Click($connectButton_OnClick)

        $form1.Controls.Add($connectButton)

        $exitButton.DataBindings.DefaultDataSourceUpdateMode = 0

        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 12
        $System_Drawing_Point.Y = 72
        $exitButton.Location = $System_Drawing_Point
        $exitButton.Name = "exitButton"
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Height = 23
        $System_Drawing_Size.Width = 260
        $exitButton.Size = $System_Drawing_Size
        $exitButton.TabIndex = 1
        $exitButton.Text = "Quit"
        $exitButton.UseVisualStyleBackColor = $True
        $exitButton.add_Click($exitButton_OnClick)

        $form1.Controls.Add($exitButton)

        $openButton.DataBindings.DefaultDataSourceUpdateMode = 0
        $openButton.Enabled = $False

        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 12
        $System_Drawing_Point.Y = 43
        $openButton.Location = $System_Drawing_Point
        $openButton.Name = "openButton"
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Height = 23
        $System_Drawing_Size.Width = 260
        $openButton.Size = $System_Drawing_Size
        $openButton.TabIndex = 0
        $openButton.Text = "Open VM Consoles"
        $openButton.UseVisualStyleBackColor = $True
        $openButton.add_Click($openButton_OnClick)

        $form1.Controls.Add($openButton)

        #endregion Generated Form Code

        #Save the initial state of the form
        $InitialFormWindowState = $form1.WindowState
        #Init the OnLoad event to correct the initial state of the form
        $form1.add_Load($OnLoadForm_StateCorrection)
        #Show the Form
        $form1.ShowDialog()| Out-Null

        } #End Function

        #Call the Function
        GenerateForm
    }
    End
    {
    }
}

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