Howto use ESXCLI in PowerCLI

PowerCLI, a set of PowerShell extensions for vSphere, is a great tool for automating VMware configuration and management tasks. It allows you to change a lot of ESXi host and vCenter settings. One powerful cmdlet is Get-EsxCli which allows you to run ESXCLI tasks from your PowerCLI console. ESXCLI is the main configuration command on an ESXi host. This post explains how to use the Get-EsxCli cmdlet.

Getting Started with Get-ESXCli

To get started with the command you have to load the output to a variable. Connect to the vCenter Server or an ESXi Host directly and run the following command. This will create an object that you can run commands against.

$esxcli = Get-EsxCli -VMhost esx01.virten.lab

The $esxcli variable can now be used in the same way as the esxcli command on an ESXi host. You can list all available namespaces by printing the variable:

To crawl through namespaces you can append any element to the $esxcli variable like you know it from esxcli. The only difference is that in PowerCLI the namespace is separated by a dot instead of a blank.

In that case, we have to define 3 variables: Enabled (boolean), force (boolean) and module (string). As an example, I am going to disable the lldp module:

enable (boolean): $false <-- This will disable the module

force (Boolean): $false <-- This will not try to force the commands execution

module (string): "lldp" <-- The name of the module to disable

Build the command:

PS C:\> $esxcli.system.module.set($false, $false, "lldp")

That's it. To get an overview of all possible esxcli commands, Robert van den Nieuwendijk has written a small script which can be found here. This script display all esxcli commands and their parameter sequence. Very handy!