End User VMware Console with PowerCLI

End User VMware Console with PowerCLI

November 11, 2013 3 By Eric Shanks

After watching Alan Renouf’s video about Open-VMConsoleWindow, I got excited about PowerShell again.  In my current job I don’t get to do much scripting anymore but wanted to give building a form for PowerCLI a try.  I’ve secretly wanted to be a programmer as long as I didn’t have to do it full time.  🙂

Using the video from Alan, and a Video from the LazyWinAdmin (included in this post) I created a fairly simple form that could run some commands on my home lab.  My main goal was get a refresher on some PowerCLI and how to use Primal Forms.

My hypothetical goal was to build a small end user VMware console that I could give to a dev team so that they could manage parts of the VMware environment, without giving them the entire console.

I first downloaded PrimalForms Community Edition so that I could put together a basic form for users.  After that, I modified the buttons to run my PowerCLI code.

 

PrimalForm1

 

 

DISCLAIMER: I am an amateur PoSH enthusiest and expect that this code could be better, more efficient and added to.  It requires PowerCLI 5.5 and a test in your lab before actually using it in your environment.  

My PowerCLI Code

[code language=powershell]
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 10/30/2013 11:04 AM
# Generated By: Eric Shanks www.theITHollow.com
# Code is provided as-is and no liability will be retained by the author.
# All Code should be tested before being used in a production environment.
########################################################################

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

#region Control Helper Functions
function Load-DataGridView
{
<#
.SYNOPSIS
This functions helps you load items into a DataGridView.

.DESCRIPTION
Use this function to dynamically load items into the DataGridView control.

.PARAMETER DataGridView
The ComboBox control you want to add items to.

.PARAMETER Item
The object or objects you wish to load into the ComboBox’s items collection.

.PARAMETER DataMember
Sets the name of the list or table in the data source for which the DataGridView is displaying data.

#>
Param (
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.DataGridView]$DataGridView,
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
$Item,
[Parameter(Mandatory=$false)]
[string]$DataMember
)
$DataGridView.SuspendLayout()
$DataGridView.DataMember = $DataMember

if ($Item -is [System.ComponentModel.IListSource]`
-or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
{
$DataGridView.DataSource = $Item
}
else
{
$array = New-Object System.Collections.ArrayList

if ($Item -is [System.Collections.IList])
{
$array.AddRange($Item)
}
else
{
$array.Add($Item)
}
$DataGridView.DataSource = $array
}

$DataGridView.ResumeLayout()
}

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$RefreshState = New-Object System.Windows.Forms.Button
$DeleteAllSnapshots = New-Object System.Windows.Forms.Button
$DeleteSnapshot = New-Object System.Windows.Forms.Button
$dataGridView1 = New-Object System.Windows.Forms.DataGridView
$RevertSnapshot = New-Object System.Windows.Forms.Button
$CreateSnapshot = New-Object System.Windows.Forms.Button
$VMToolsUpgrade = New-Object System.Windows.Forms.Button
$ShutdownGuest = New-Object System.Windows.Forms.Button
$PowerOff = New-Object System.Windows.Forms.Button
$PowerON = New-Object System.Windows.Forms.Button
$OpenConsole = New-Object System.Windows.Forms.Button
$VMLabel = New-Object System.Windows.Forms.Label
$textBox1vcenter = New-Object System.Windows.Forms.TextBox
$ConnectvCenter = 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.

$RevertSnapshot_OnClick=
{
#Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
#List Snapshots available
$Snap2Revert = Get-VM $GRIDCell | Get-Snapshot | Out-GridView -outputmode single
# Set VM to snapshot
Set-VM -VM $GRIDCell -Snapshot $Snap2Revert -confirm:$false
}

$RefreshState_OnClick=
{
#Populate the list of VMs in the DataGrid on the Form
$VMLIST = Get-VM | Select-Object Name, Powerstate
Load-DataGridView -DataGridView $dataGridView1 -Item $VMLIST
}
$PowerOff_OnClick=
{
#Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
#Check the State of the VM to see if it’s ON
$VMState = get-vm -Name $GRIDCell
$PowerState = $VMState.PowerState
if ($PowerState -like “PoweredOn”)
{
Stop-VM $GRIDCell
}
}

$CreateSnapshot_OnClick=
{
##Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
$today = Get-Date
#$SNAPName = “USERConsoleSnap ” + $today
$SNAPName = Read-Host “Snapshot Name?”
$SnapDescript = Read-Host “Snapshot Description?”
#New-Snapshot -VM $GRIDCell -Name $SNAPName -Description $today
New-Snapshot -VM $GRIDCell -Name $SNAPName -Description $SnapDescript
}

$OpenConsole_OnClick=
{
#Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
#Check the State of the VM to see if it’s ON
$VMState = get-vm -Name $GRIDCell
$PowerState = $VMState.PowerState
if ($PowerState -like “PoweredOn”)
{
#### vSphere 5.5 or higher should be used for the open-vmconsolewindow command to work.
Open-VMConsoleWindow $GRIDCell
}
}

$ShutdownGuest_OnClick=
{
#Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
#Check the State of the VM to see if it’s ON
$VMState = get-vm -Name $GRIDCell
$PowerState = $VMState.PowerState
if ($PowerState -like “PoweredOn”)
{
Shutdown-VMGuest -VM $GRIDCell
}
}

$PowerON_OnClick=
{
#Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
#Check the State of the VM to see if it’s ON
$VMState = get-vm -Name $GRIDCell
$PowerState = $VMState.PowerState
if ($PowerState -like “PoweredOff”)
{
Start-VM $GRIDCell
}

}

$DeleteAllSnapshots_OnClick=
{
#Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
Get-VM $GRIDCell | Get-Snapshot | Remove-Snapshot -confirm:$false -RunAsync
}

$DeleteSnapshot_OnClick=
{
#Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
$Snap2Delete = Get-VM $GRIDCell | Get-Snapshot | Out-GridView -outputmode single
Get-Snapshot -VM $GRIDCell -name $Snap2Delete | Remove-Snapshot -ErrorAction SilentlyContinue
}

$VMToolsUpgrade_OnClick=
{
#Grab Selection from DataGrid
$GRIDCell = $dataGridView1.currentrow.Cells[0].value
Get-VM $GRIDCELL | Update-Tools –NoReboot
}

$ConnectvCenter_OnClick=
{
#Connect to a vCenter Server
Connect-VIServer $textBox1vcenter.text
#Populate the list of VMs in the DataGrid on the Form
$VMLIST = Get-VM | Select-Object Name, Powerstate
Load-DataGridView -DataGridView $dataGridView1 -Item $VMLIST
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
#Load the VMware Snapin if not already loaded
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin VMware.VimAutomation.Core
}
}

#———————————————-
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,119,136,153)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 512
$System_Drawing_Size.Width = 554
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = “form1”
$form1.Text = “theITHollow User Console”

$DeleteAllSnapshots.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 433
$System_Drawing_Point.Y = 351
$DeleteAllSnapshots.Location = $System_Drawing_Point
$DeleteAllSnapshots.Name = “DeleteAllSnapshots”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 35
$System_Drawing_Size.Width = 110
$DeleteAllSnapshots.Size = $System_Drawing_Size
$DeleteAllSnapshots.TabIndex = 15
$DeleteAllSnapshots.Text = “Delete All Snapshots”
$DeleteAllSnapshots.UseVisualStyleBackColor = $True
$DeleteAllSnapshots.add_Click($DeleteAllSnapshots_OnClick)

$form1.Controls.Add($DeleteAllSnapshots)

$DeleteSnapshot.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 274
$System_Drawing_Point.Y = 351
$DeleteSnapshot.Location = $System_Drawing_Point
$DeleteSnapshot.Name = “DeleteSnapshot”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 35
$System_Drawing_Size.Width = 121
$DeleteSnapshot.Size = $System_Drawing_Size
$DeleteSnapshot.TabIndex = 14
$DeleteSnapshot.Text = “DeleteSnapshot”
$DeleteSnapshot.UseVisualStyleBackColor = $True
$DeleteSnapshot.add_Click($DeleteSnapshot_OnClick)

$form1.Controls.Add($DeleteSnapshot)

$dataGridView1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 60
$System_Drawing_Point.Y = 102
$dataGridView1.Location = $System_Drawing_Point
$dataGridView1.Name = “dataGridView1”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 300
$System_Drawing_Size.Width = 200
$dataGridView1.Size = $System_Drawing_Size
$dataGridView1.TabIndex = 12

$form1.Controls.Add($dataGridView1)

$RevertSnapshot.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 433
$System_Drawing_Point.Y = 304
$RevertSnapshot.Location = $System_Drawing_Point
$RevertSnapshot.Name = “RevertSnapshot”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 32
$System_Drawing_Size.Width = 110
$RevertSnapshot.Size = $System_Drawing_Size
$RevertSnapshot.TabIndex = 11
$RevertSnapshot.Text = “RevertSnapshot”
$RevertSnapshot.UseVisualStyleBackColor = $True
$RevertSnapshot.add_Click($RevertSnapshot_OnClick)

$form1.Controls.Add($RevertSnapshot)

$CreateSnapshot.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 274
$System_Drawing_Point.Y = 304
$CreateSnapshot.Location = $System_Drawing_Point
$CreateSnapshot.Name = “CreateSnapshot”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 32
$System_Drawing_Size.Width = 121
$CreateSnapshot.Size = $System_Drawing_Size
$CreateSnapshot.TabIndex = 10
$CreateSnapshot.Text = “Create Snapshot”
$CreateSnapshot.UseVisualStyleBackColor = $True
$CreateSnapshot.add_Click($CreateSnapshot_OnClick)

$form1.Controls.Add($CreateSnapshot)

$VMToolsUpgrade.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 274
$System_Drawing_Point.Y = 224
$VMToolsUpgrade.Location = $System_Drawing_Point
$VMToolsUpgrade.Name = “VMToolsUpgrade”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 32
$System_Drawing_Size.Width = 121
$VMToolsUpgrade.Size = $System_Drawing_Size
$VMToolsUpgrade.TabIndex = 9
$VMToolsUpgrade.Text = “VMToolsUpgrade”
$VMToolsUpgrade.UseVisualStyleBackColor = $True
$VMToolsUpgrade.add_Click($VMToolsUpgrade_OnClick)

$form1.Controls.Add($VMToolsUpgrade)

$ShutdownGuest.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 433
$System_Drawing_Point.Y = 102
$ShutdownGuest.Location = $System_Drawing_Point
$ShutdownGuest.Name = “ShutdownGuest”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 106
$ShutdownGuest.Size = $System_Drawing_Size
$ShutdownGuest.TabIndex = 8
$ShutdownGuest.Text = “ShutdownGuest”
$ShutdownGuest.UseVisualStyleBackColor = $True
$ShutdownGuest.add_Click($ShutdownGuest_OnClick)

$form1.Controls.Add($ShutdownGuest)

$PowerOff.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 345
$System_Drawing_Point.Y = 102
$PowerOff.Location = $System_Drawing_Point
$PowerOff.Name = “PowerOff”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 75
$PowerOff.Size = $System_Drawing_Size
$PowerOff.TabIndex = 7
$PowerOff.Text = “PowerOff”
$PowerOff.UseVisualStyleBackColor = $True
$PowerOff.add_Click($PowerOff_OnClick)

$form1.Controls.Add($PowerOff)

$PowerON.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 264
$System_Drawing_Point.Y = 102
$PowerON.Location = $System_Drawing_Point
$PowerON.Name = “PowerON”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 75
$PowerON.Size = $System_Drawing_Size
$PowerON.TabIndex = 6
$PowerON.Text = “PowerON”
$PowerON.UseVisualStyleBackColor = $True
$PowerON.add_Click($PowerON_OnClick)

$form1.Controls.Add($PowerON)

$OpenConsole.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 274
$System_Drawing_Point.Y = 170
$OpenConsole.Location = $System_Drawing_Point
$OpenConsole.Name = “OpenConsole”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 34
$System_Drawing_Size.Width = 121
$OpenConsole.Size = $System_Drawing_Size
$OpenConsole.TabIndex = 5
$OpenConsole.Text = “Open Console”
$OpenConsole.UseVisualStyleBackColor = $True
$OpenConsole.add_Click($OpenConsole_OnClick)

$form1.Controls.Add($OpenConsole)

$VMLabel.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 60
$System_Drawing_Point.Y = 73
$VMLabel.Location = $System_Drawing_Point
$VMLabel.Name = “VMLabel”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 26
$System_Drawing_Size.Width = 200
$VMLabel.Size = $System_Drawing_Size
$VMLabel.TabIndex = 3
$VMLabel.Text = “Virtual Machines”
$VMLabel.TextAlign = 2
$VMLabel.add_Click($handler_VMLabel_Click)

$form1.Controls.Add($VMLabel)

$textBox1vcenter.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 178
$System_Drawing_Point.Y = 30
$textBox1vcenter.Location = $System_Drawing_Point
$textBox1vcenter.Name = “textBox1vcenter”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 161
$textBox1vcenter.Size = $System_Drawing_Size
$textBox1vcenter.TabIndex = 2
$textBox1vcenter.Text = “vCenter.hollow.local”

$form1.Controls.Add($textBox1vcenter)

$ConnectvCenter.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 60
$System_Drawing_Point.Y = 30
$ConnectvCenter.Location = $System_Drawing_Point
$ConnectvCenter.Name = “ConnectvCenter”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$ConnectvCenter.Size = $System_Drawing_Size
$ConnectvCenter.TabIndex = 1
$ConnectvCenter.Text = “Connect vCenter”
$ConnectvCenter.UseVisualStyleBackColor = $True
$ConnectvCenter.add_Click($ConnectvCenter_OnClick)

$form1.Controls.Add($ConnectvCenter)

$RefreshState.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 60
$System_Drawing_Point.Y = 437
$RefreshState.Location = $System_Drawing_Point
$RefreshState.Name = “RefreshState”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 200
$RefreshState.Size = $System_Drawing_Size
$RefreshState.TabIndex = 16
$RefreshState.Text = “Refresh VM State”
$RefreshState.UseVisualStyleBackColor = $True
$RefreshState.add_Click($RefreshState_OnClick)

$form1.Controls.Add($RefreshState)

#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

[/code]
If you also would like to see some very brief but useful videos on how to do this, I’ve embedded them below as well as the VMware documentation on Open-VMConsoleWindow.

I’m sure that many improvements can be made to this.  Please share your tweaks, thoughts and how you’ve used this in the comments sections of this post!

http://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2FOpen-VMConsoleWindow.html