My notes and ramblings, normally about automation

PowerCLI - Code Capture

· Read in about 3 min · (597 Words)
PowerCLI PowerShell VMware

Something amazing has 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

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!

Let’s checkout Code Capture!

Code Capture in Action

Code Capture In Action

I’m really liking the syntax highlighting, very helpful!

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 -----------------

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: Code Capture Copy Paste Output

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: Code Capture Using Downloaded Script

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.

Add this to your environment by downloading the latest version of the vSphere HTML5 Web Client Fling!