In this post I will discuss about managing ESXi hosts using ESXCLI and PowerCLI. Yes, You got it right. ESXCLI is one of the widely used command line interface for ESXi whereas PowerCLI is a PowerShell based snap-in for VMware.
ESXCLI is intended to provide a single set of commands to perform host based administrative tasks. ESXCLI has set of namespace for various components like network, storage, device etc. Refer below pic to see available namespaces.
How to run ESXCLI?
Well working with ESXCLI is very simple. Take a putty session to host and type esxcli. Once you run this command then it will show you all the available namespace under ESXCLI. Further more you can type child namespace and it will show you available namespace and commands under that.
# esxcli network nic list
The above command will list NIC details (MAC, Adapter type, Link status etc.)
When you have very small environment or performing specific tasks on 1-2 esxi then taking a putty session and running ESXCLI command is not a big pain. However there are scenarios where you want to perform management tasks on large number of ESXi hosts then above approach may not be a feasible one. Such scenarios could be-
- Getting certain reports across all the hosts (Firmware, driver version or specific configuration detail)
- Setting up standard configuration across multiple hosts or entire environment.
So in such type of scenario you would like to automate the task. For that obvious choice would be PowerCLI.
Tip: PowerCLI is a PowerShell snap-in for VMware. There is no specific training required for PowerCLI.
How to execute ESXCLI Commands using PowerCLI?
PowerCLI has a cmdlet called Get-ESXCLI, This cmdlet can be executed against remote ESXi hosts. This can be done by passing a value (hostname) to -VMhost parameter. Refer below command-
PowerCLI C:\> Get-Esxcli -VMhost TestEsxi.poc
The above command will give you available ESXCLI namespace on TestEsxi.poc host.
For a moment let us consider Ex 1. ~
# esxcli network nic list
Same result can be achieved via executing below command in PowerCLI.
PowerCLI C:> (Get-Esxcli -VMhost "TestEsxi.poc").network.nic.list()
How to automate same task for multiple hosts?
Below is the PS script which will automate same task for multiple host. I don’t think that below script needs any explanation, It is very simple and straight forward script. First, You Get content from a .txt file. This file contains ESXi Host names. In later part you are just taking hostname one by one and dynamically passing it to cmdlet.
Please refer below screenshot for same, Though it does not harm anything to your environment but still i would highly recommend you to run this in POC first. Also please note that you have to connect vCenter first.
PowerCLI C:> Connect-VIServer -Server vCenterName
ESXCLI is a great tool for ESXi management and capability to integrate with PowerShell is just awesome. Next time if you have to collect few reports from multiple ESXi hosts then try to automate those reports using PowerCLI and ESXCLI.
- How to Learn PowerShell?
- How to install PowerCLI within PowerShell Console?
- Check out this blog for your reference
Hope you liked this post, Please comment and share your feedback in comment section.