Power control of VMs on ESXi using a script on the command line

Automating VM power control over the command line can be very useful. Especially to simplify test scenarios when tens or hundreds of VM’s are used. A couple of simple examples below.

Unfortunately the commands for powering on and off are completely different. This means we have two separate scripts for each task:

Powering on:
List all VM’s so we can find the ones we want:

Grep for the ones we are interested in:

Print only their IDs:

Because we’re pedantic – sort them in correct order – just in case:

Run a loop to power them on:

Powering off:
List all the processes:
By default esxcli lists the information on separate lines which makes scripting close to impossible. Therefore we use the –formatter option to list the output in CSV format.

To enable AWK we replace spaces with % (for example) and commas with spaces:

Grep for the VMs we want to power off:

Print only the world-id’s:

Run it in a loop:

Leave a Reply