The ESXCLI command is used to manage many aspects of an ESXi host. You can run ESXCLI commands as vCLI commands, in an SSH session or run them in the ESXi Shell in troubleshooting situations. Even though VMware keeps growing the PowerCLI cmdlet library, there are still quite a lot of things you can do using ESXCLI that you can’t do in PowerCLI. Thankfully the PowerCLI modules include the Get-EsxCLI cmdlet that allows you to run esxcli commands through PowerShell.
To use it you need to add a host as parameter and store the result in a variable. Don’t forget to add the -V2 switch. The -V2 switch was introduced in PowerCLI 6.3R1 and makes its usage a lot easier.
Invoke() method
Before starting I need to introduce these 2 methods. This method is used to launch the esxcli command against the host. It is comparable to pressing enter in the ESXi shell on an esxcli command. It sometimes needs arguments when the command’s option requires mandatory parameters.
1. CreateArgs() method
When an Invoke() method requires arguments (parameters), the CreateArgs() method is available on the object. When you create an ‘arguments variable’ using it, it will create a hash table with all the parameters you can pass to the command. Very handy when you are not familiar with it. then you need to use the Invoke() method with the arguments variable between the parenthesis like so $esxcli……Invoke($MyArgs).
How to use it
In the present example we will check if IPv6 is enabled on a host. The esxcli command to achieve this is the following:
Create the EsxCLI variable
You first need to run the Get-EsxCLI cmdlet with a host in the parameters and the V2 switch and store the output in a variable. If you look at the object type of the $esxcli variable, you will see that it has its own type EsxCliImpl.
If you display the content of the variable you will see the same options you would get in the shell of a host.
PS> $esxcli
==================================
EsxCli: mg-p-esxcs31.mgmtdom.intra
Elements:
———
device
elxfc
elxfcoe
elxnet
esxcli
fcoe
graphics
hardware
iscsi
network
nvme
rdma
sched
software
storage
system
vm
vsan
You can browse through the properties of the object. Whenever you press enter on a property it will display a table of information.
Method Elements : Where you can go (property or esxcli option)
PS> $esxcli.system.module.parameters
=========================
EsxCliElement: parameters
Method Elements:
———
copy
list
set
Methods:
——–
string Help()
Methods : What you can do including is, whether the Invoke() command requires parameters or not. In which case a parameters object must be created using the CreateArgs() method. Below you can see that the invoke method of the list element does require and arguments hash table.
PS> $esxcli.system.module.parameters.list
===================
EsxCliMethodElement: list
Methods:
——–
Hashtable CreateArgs()
ModuleParameter
string Help()
2. Create the arguments variable
We replicated the esxcli command we mentioned earlier in the $esxcli variable. We also saw that the Invoke() method of the the list element requires parameters. This is what we create here in this $arguments variable.
If you look at its content you will see all the properties you can set and the type it expects. This list element only takes the module parameter.
PS> $arguments
Name Value
—- —–
module Unset, ([string])
3. Configure the arguments variable
Now we need to set the module parameter according to the esxcli command (esxcli system module parameters list -m tcpip4).
4. Invoke the command with arguments
It is time to start the command for real. All that is left to do is to run the invoke method with the argument variable as parameters and it will display the same as what esxcli would. Below we can see that ipv6 is disabled.
PS> $esxcli.system.module.parameters.list.Invoke($arguments)
Description | Name | Type Value |
# of provisioned H/W iSCSI ports | ipportIscsiReserved | int |
Enable/Disable IPv6 | ipv6 | int 0 |
Conclusion
This example was a simple one but it doesn’t get a lot more complicated than that. You may have to specify more parameters but that’s about it. You can find everything you need about the esxcli commands in the vSphere Command-Line Interface Reference.
Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.