Code Capture in Developer Center

In this post I will show you how to use the Code Capture feature in the Developer Center which is present in vCenter since version 6.7u2. Code Capture is a powerfull tool which records user actions  and translates them into executable code.

Code capture gives you te ability  to record actions taken in the vSphere Client and output them as usable PowerCLI code. You can then copy the code or download it as a script and use it in a PowerShell session to execute the task.

To demonstrate the working of the Code Capture feature I will record the creation of a snapshot of a VM and changing the number of vCPU from 2 to 4.

First I have to enable the Code Capture feature:

Navigate to Developer Center:

Select the Code Capture tab:

Enable Code Capture and start recording:

It is also possible to use this button to stop and start recording:

The next step is to take a snapshot named “test_snapshot” and increase the numbers of vCPU from 2 to 4 of a VM using the UI of the vSphere Client. Afer doing this I will press the stop button and I will be redirect to the Code Capture section in Developer Center

The following code is captured:

#----------------- Start of code capture -----------------

$name = 'test_snapshot'
$memory = $false
$quiesce = $false
$_this = Get-View -Id 'VirtualMachine-vm-1814'
$_this.CreateSnapshot_Task($name, $null, $memory, $quiesce)

$_this = Get-View -Id 'VirtualMachine-vm-1814'

$_this = Get-View -Id 'VirtualMachine-vm-1814'

$spec = New-Object VMware.Vim.EnvironmentBrowserConfigOptionQuerySpec
$spec.GuestId = New-Object String[] (1)
$spec.GuestId[0] = 'windows9Server64Guest'
$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'

$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'

$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'

$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'

$_this = Get-View -Id 'CryptoManagerKmip-CryptoManager'

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.CpuAllocation = New-Object VMware.Vim.ResourceAllocationInfo
$spec.CpuAllocation.Shares = New-Object VMware.Vim.SharesInfo
$spec.CpuAllocation.Shares.Shares = 4000
$spec.CpuAllocation.Shares.Level = 'normal'
$spec.NumCPUs = 4
$spec.DeviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (0)
$spec.CpuFeatureMask = New-Object VMware.Vim.VirtualMachineCpuIdInfoSpec[] (0)
$_this = Get-View -Id 'VirtualMachine-vm-1814'

$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'

#----------------- End of code capture -----------------



As you can see this is very helpfull. The captured code can be used/copied in your Powershell/PowerCLI script so you can automate anything which is vSphere related. For example: Creating/Cloning Virtual Machines, changing the config of existing VM’s/Hosts/Cluster and so on.


Introduction to Code Capture:


Roderik de Block








Roderik de Block

Leave a Reply

Your email address will not be published. Required fields are marked *