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:

[root@vGPU-ESXi60-01:~] vim-cmd vmsvc/getallvms 
Vmid         Name                                  File                                 Guest OS          Version             Annotation           
1      vcenter60           [datastore1] vcenter60/vcenter60.vmx                   sles11_64Guest          vmx-08    VMware vCenter Server Appliance
13     VMW-vGPU-02         [datastore1] VMW-vGPU-02/VMW-vGPU-02.vmx               windows7_64Guest        vmx-11                                   
14     VMW-vGPU-01         [datastore1] VMW-vGPU-01/VMW-vGPU-01.vmx               windows7_64Guest        vmx-11                                   
15     Win2012R2-vGPU      [datastore1] Win2012R2-vGPU/Win2012R2-vGPU.vmx         windows8Server64Guest   vmx-11                                   
3      HorizonView6.1_CS   [datastore1] HorizonView6.1_CS/HorizonView6.1_CS.vmx   windows8Server64Guest   vmx-11                                   
[root@vGPU-ESXi60-01:~] 

Grep for the ones we are interested in:

[root@vGPU-ESXi60-01:~] vim-cmd vmsvc/getallvms | grep vGPU
13     VMW-vGPU-02         [datastore1] VMW-vGPU-02/VMW-vGPU-02.vmx               windows7_64Guest        vmx-11                                   
14     VMW-vGPU-01         [datastore1] VMW-vGPU-01/VMW-vGPU-01.vmx               windows7_64Guest        vmx-11                                   
15     Win2012R2-vGPU      [datastore1] Win2012R2-vGPU/Win2012R2-vGPU.vmx         windows8Server64Guest   vmx-11                                   
[root@vGPU-ESXi60-01:~] 

Print only their IDs:

[root@vGPU-ESXi60-01:~] vim-cmd vmsvc/getallvms | grep vGPU | awk '{print $1}'
13
14
15
[root@vGPU-ESXi60-01:~] 

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

[root@vGPU-ESXi60-01:~] vim-cmd vmsvc/getallvms | grep vGPU | awk '{print $1}' | sort -n
13
14
15
[root@vGPU-ESXi60-01:~] 

Run a loop to power them on:

[root@vGPU-ESXi60-01:~] for i in `vim-cmd vmsvc/getallvms | grep vGPU | awk '{print $1}' | sort -n`
> do
> echo "Powering on number $i"
> vim-cmd vmsvc/power.on $i
> done

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.

[root@vGPU-ESXi60-01:~] esxcli --formatter=csv vm process list
ConfigFile,DisplayName,ProcessID,UUID,VMXCartelID,WorldID,
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/vcenter60/vcenter60.vmx,vcenter60,0,56 4d eb 29 84 8f 47 7d-4a 9f fe 80 3e b7 3d 59,1380839,1380840,
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/VMW-vGPU-01/VMW-vGPU-01.vmx,VMW-vGPU-01,0,42 22 22 66 b1 22 29 73-c2 e6 ce b6 44 74 b4 24,1382270,1382271,
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/HorizonView6.1_CS/HorizonView6.1_CS.vmx,HorizonView6.1_CS,0,42 22 43 6b 80 77 c1 56-ac 2c 07 42 d3 74 bb e3,1382369,1382370,
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/Win2012R2-vGPU/Win2012R2-vGPU.vmx,Win2012R2-vGPU,0,42 22 c2 1b f2 b1 2c f2-d8 55 da df 9a 26 15 71,1382460,1382461,
[root@vGPU-ESXi60-01:~]

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

[root@vGPU-ESXi60-01:~] esxcli --formatter=csv vm process list | sed 's/ /%/g' | sed 's/,/ /g'
ConfigFile DisplayName ProcessID UUID VMXCartelID WorldID 
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/vcenter60/vcenter60.vmx vcenter60 0 56%4d%eb%29%84%8f%47%7d-4a%9f%fe%80%3e%b7%3d%59 1380839 1380840 
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/VMW-vGPU-01/VMW-vGPU-01.vmx VMW-vGPU-01 0 42%22%22%66%b1%22%29%73-c2%e6%ce%b6%44%74%b4%24 1382270 1382271 
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/HorizonView6.1_CS/HorizonView6.1_CS.vmx HorizonView6.1_CS 0 42%22%43%6b%80%77%c1%56-ac%2c%07%42%d3%74%bb%e3 1382369 1382370 
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/Win2012R2-vGPU/Win2012R2-vGPU.vmx Win2012R2-vGPU 0 42%22%c2%1b%f2%b1%2c%f2-d8%55%da%df%9a%26%15%71 1382460 1382461 
[root@vGPU-ESXi60-01:~]

Grep for the VMs we want to power off:

[root@vGPU-ESXi60-01:~] esxcli --formatter=csv vm process list | sed 's/ /%/g' | sed 's/,/ /g' | grep vGPU
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/VMW-vGPU-01/VMW-vGPU-01.vmx VMW-vGPU-01 0 42%22%22%66%b1%22%29%73-c2%e6%ce%b6%44%74%b4%24 1382270 1382271 
/vmfs/volumes/55015477-1d4dd911-786f-b083feca800f/Win2012R2-vGPU/Win2012R2-vGPU.vmx Win2012R2-vGPU 0 42%22%c2%1b%f2%b1%2c%f2-d8%55%da%df%9a%26%15%71 1382460 1382461 
[root@vGPU-ESXi60-01:~]

Print only the world-id’s:

[root@vGPU-ESXi60-01:~] esxcli --formatter=csv vm process list | sed 's/ /%/g' | sed 's/,/ /g' | grep vGPU | awk '{print $6}'
1382271
1382461
[root@vGPU-ESXi60-01:~]

Run it in a loop:

[root@vGPU-ESXi60-01:~] for i in `esxcli --formatter=csv vm process list | sed 's/ /%/g' | sed 's/,/ /g' | grep vGPU | awk '{print $6}'`
> do
> esxcli vm process kill --type=soft --world-id=$i
> done
[root@vGPU-ESXi60-01:~]

Leave a Reply