Using ESXCLI Commands in Scripts
Last Updated December 16, 2024

Most administrators run scripts to perform the same task repeatedly or to perform a task on multiple hosts. You can run ESXCLI commands from one administration server against multiple target servers.
For example, when a new data store becomes available in your environment, you must make that data store available to each
ESXi
host. The following sample script illustrates how to make a NAS data store available to three hosts (
esxi_server_a, esx_server_b, and esxi_server_c
).
The sample assumes that a configuration file
/home/admin/.visdkrc.<
hostname
>
exists for each host. For example, the configuration file for
esxi_server_a
has the following contents.
VI_SERVER = esxi_server_a
VI_USERNAME = root
VI_PASSWORD = xysfdjkat
The script adds the NAS data store to each host defined in
VIHOSTS
.
#!/bin/bash
 
VI_CONFIG_FILE=/home/admin/viconfig
VIHOSTS=(esxi_server_a esx_server_b esxi_server_c)
 
 
for VIHOST in ${VIHOSTS[@]}
do
echo "Adding NAS datastore for ${VIHOST} ..."
esxcli --config ${VI_CONFIG_FILE} storage nfs add --host ${VIHOST} --share <share point> --volume-name <volume name>
esxcli --config ${VI_CONFIG_FILE} storage nfs list
done