0

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

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

#---------------EnvironmentBrowser---------------
$_this = Get-View -Id 'VirtualMachine-vm-1814'
$_this.EnvironmentBrowser

#---------------Config---------------
$_this = Get-View -Id 'VirtualMachine-vm-1814'
$_this.Config

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

#---------------DatastoreBrowser---------------
$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'
$_this.DatastoreBrowser

#---------------QueryTargetCapabilities---------------
$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'
$_this.QueryTargetCapabilities($null)

#---------------QueryConfigOptionDescriptor---------------
$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'
$_this.QueryConfigOptionDescriptor()

#---------------ListKmipServers---------------
$_this = Get-View -Id 'CryptoManagerKmip-CryptoManager'
$_this.ListKmipServers($null)

#---------------ReconfigVM_Task---------------
$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.ReconfigVM_Task($spec)

#---------------QueryConfigOptionDescriptor---------------
$_this = Get-View -Id 'EnvironmentBrowser-envbrowser-1814'
$_this.QueryConfigOptionDescriptor()


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

 

Conclusion:

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:

https://blogs.vmware.com/code/2019/03/19/talking-code-code-capture/

Roderik de Block

 

 

 

 

 

 

 

Roderik de Block

Leave a Reply

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