I have recently been doing a lot of work around providing Console access to VMs sitting in a vSphere environment.  This has been made a lot easier with the inclusion in the more recent versions of the vSphere web client of a console that runs over web sockets in HTML5 as opposed to the earlier VMRC plug-in which would only run on windows and required the plug-in to be installed.

As the new console runs over HTML5 it will run in any modern browser on any OS, (Although we are having problems getting text input to the console on mobile devices.)  There is also a great article be William Lam on Virtually Ghetto on how to generate the URL with a Perl script. http://www.virtuallyghetto.com/2013/09/how-to-generate-pre-authenticated-html5.html.

I have converted William’s efforts to a PowerShell script, as this fits better with the system we are using, so am publishing it here in case it helps anyone else looking in to this who are more comfortable with PowerShell than Perl or just save someone else the work of converting it.

The script below will work with vSphere 5.5 and 5.5 update 1.  In update 2 and later the console defaults to HTTPS.  To work with update 2 change the ConsolePort variable from 7331 to 7343 and change $ConsoleUrl from HTTP to HTTPS.

The SSL thumbprint at the end of $ConsoleUrl will need to be changed to match the thumbprint for your vCenter server. This can be automated with OpenSSL.  But I did not find a need to do this as the thumbprint should not change.

$vCenterFQDN = "vcenter IP or FQDN"
$ConsolePort = 7331 
$VM = "VM Name"
$VM = Get-VM $VM
$VMMoRef = $VM.ExtensionData.MoRef.Value

Connect-VIServer $vCenterFQDN

#Get vCenterFQDN from advanced settings
$SettingsMgr = Get-View $global:DefaultVIServer.ExtensionData.Client.ServiceContent.Setting
$Settings = $SettingsMgr.Setting.GetEnumerator() 
$AdvancedSettingsFQDN = ($Settings | Where {$_.Key -eq "VirtualCenter.FQDN" }).Value

#Get vCenter ticket
$SessionMgr = Get-View $global:DefaultVIServer.ExtensionData.Client.ServiceContent.SessionManager
$Session = $SessionMgr.AcquireCloneTicket()

#Create URL

$ConsoleURL = "http://$($vCenterFQDN):$($ConsolePort)/console/?vmId=$($VMMoRef)&vmName=$($VM.Name)&host=$($AdvancedSettingsFQDN)&sessionTicket=$($Session)&thumbprint=E5:F4:E3:AA:0F:47:1F:A7:58:16:83:C4:7D:FF:06:B6:81:B4:CF:8E"

Write-Output $ConsoleURL

Comments