I am extremely excited to announce a new feature that’s been added to the vSphere HTML5 Web Client Fling. This new feature is called Code Capture. While the name might not sound familiar, hopefully you’re already acquainted with its predecessor – Onyx, which is currently the most requested feature on the PowerCLI Feature Requests site!

Code Capture gives you the ability to take actions you’ve completed in the vSphere Client and outputs usable code. Once you have the vSphere HTML5 Web Client Fling installed, it’s just as simple as hitting the red ‘record’ button on the top menu, performing your activities, then hitting the red ‘stop’ button. At this point, you’ll be taken to the Code Capture section where you can browse your code and even copy or download the code as well!

Before jumping to download the fling, we should discuss what a VMware Fling really is. Flings are projects that are built by our engineers and made available for general consumption. These flings are distributed under the Technical Preview License and, therefore, are recommended to not be run on production environments. This also means there is no ability to open a VMware support request. However, you can open a bug against the fling itself using the ‘Bugs‘ tab. Also, I would like to point out the ‘Comments‘ tab where you can submit feedback. We would greatly appreciate any and all feedback you have!

Now, let’s checkout Code Capture!

Code Capture in Action

Now let’s take a more in-depth look.

Code Capture Output

In the example above, we can see that we are shutting down a system by the name of ‘app01’. Once we click on the ‘stop’ button, it shows us the PowerCLI.NET code. This is important because, at first glance, the output may not be quite as you expect.

For example, you may have expected to see a command like Shutdown-VMGuest -Name app01 Instead, we saw low-level PowerCLI calls. Walking through the code, it created a VirtualMachine object, which referenced the app01 VM by MoRef, followed by the usage of a method named ‘ShutdownGuest’ that is called directly against the VM object. More information about this specific method: ShutdownGuest()

Code Output:

#----------------- Start of code capture ----------------- <# .SYNOPSIS Gets VI server connection by a given server uuid. .DESCRIPTION Gets VI server connection by a given server instance uuid from the default connected VI servers collection. #> function Get-VcConnection([string]$VcInstanceUuid) { $DefaultVIServers | Where-Object {$_.InstanceUuid -eq $vcInstanceUuid} } #---------------ParentVApp--------------- $_this = Get-View -Id 'VirtualMachine-vm-626' -Server (Get-VcConnection -VcInstanceUuid '87b30cb3-72b0-46f1-afb2-556824647b85') $_this.ParentVApp #---------------ShutdownGuest--------------- $_this = Get-View -Id 'VirtualMachine-vm-626' -Server (Get-VcConnection -VcInstanceUuid '87b30cb3-72b0-46f1-afb2-556824647b85') $_this.ShutdownGuest() #----------------- End of code capture ----------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #----------------- Start of code capture ----------------- <# .SYNOPSIS Gets VI server connection by a given server uuid. .DESCRIPTION Gets VI server connection by a given server instance uuid from the default connected VI servers collection. #> function Get-VcConnection ( [ string ] $VcInstanceUuid ) { $DefaultVIServers | Where-Object { $_ . InstanceUuid -eq $vcInstanceUuid } } #---------------ParentVApp--------------- $_this = Get-View -Id 'VirtualMachine-vm-626' -Server ( Get-VcConnection -VcInstanceUuid '87b30cb3-72b0-46f1-afb2-556824647b85' ) $_this . ParentVApp #---------------ShutdownGuest--------------- $_this = Get-View -Id 'VirtualMachine-vm-626' -Server ( Get-VcConnection -VcInstanceUuid '87b30cb3-72b0-46f1-afb2-556824647b85' ) $_this . ShutdownGuest ( ) #----------------- End of code capture -----------------

It’s worth noting, at this point in time, all of the returned code is going to be in this manner. There are on-going investigations to add the translation from this code into high-level PowerCLI cmdlets. If this type of conversion is important to you, please add a comment to the following PowerCLI Feature Request: Onyx support for HTML5 and REST APIs

Using the Output

If I were to power on the app01 VM, I could then use the code in a PowerShell session (where I’m already connected to the vCenter Server) to shutdown the app01 system. To make this easier, Code Capture includes a ‘Copy’ button which easily copies the entirety of the code to our clipboard. We can now paste this into our PowerShell session to accomplish the task!

Example:



Another option to extract the code from our browser is with the ‘Download’ button. Clicking ‘Download’ will, depending on your browser configuration, save a file named ‘power-cli-script.ps1’ to your Downloads folder. We can then call that file just like any script from our PowerShell session to accomplish the action.

Example:



Summary

The next generation of Onyx is here and it’s called Code Capture, which is only available in the vSphere HTML5 Web Client Fling! Using the start and stop button on the main menu bar, Code Capture allows you to take your actions in the vSphere Client and output them to PowerCLI code. The code output can also easily be copied and/or downloaded from your browser session using the dedicated buttons in the Code Capture area.

Get started by heading over to the vSphere HTML5 Web Client Fling page and downloading the latest version today!