Automating VM Power Control on ESXi via Command Line
Automating virtual machine power operations via the command line can be extremely useful, especially when managing large environments with tens or hundreds of VMs. This guide demonstrates how to script power operations for ESXi virtual machines.
Overview
Unfortunately, the commands for powering on and off VMs are completely different in ESXi:
- Power on: Uses the
vim-cmd
command
- Power off: Uses the
esxcli
command
This means we need separate approaches for each operation.
Powering On VMs
Step 1: List All VMs to Find Target VMs
1
| vim-cmd vmsvc/getallvms
|
Example output:
1
2
3
4
5
6
| 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
|
Step 2: Filter for Specific VMs
1
| vim-cmd vmsvc/getallvms | grep vGPU
|
Example output:
1
2
3
| 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
|
1
| vim-cmd vmsvc/getallvms | grep vGPU | awk '{print $1}'
|
Example output:
Step 4: Sort the VM IDs (Optional)
1
| vim-cmd vmsvc/getallvms | grep vGPU | awk '{print $1}' | sort -n
|
Step 5: Power On VMs in a Loop
1
2
3
4
5
| for i in `vim-cmd vmsvc/getallvms | grep vGPU | awk '{print $1}' | sort -n`
do
echo "Powering on VM ID $i"
vim-cmd vmsvc/power.on $i
done
|
Powering Off VMs
Step 1: List Running VM Processes
Use the CSV formatter to make the output easier to parse:
1
| esxcli --formatter=csv vm process list
|
Example output:
1
2
3
4
5
| 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,
|
Replace spaces with % and commas with spaces to make it easier to process with awk:
1
| esxcli --formatter=csv vm process list | sed 's/ /%/g' | sed 's/,/ /g'
|
Example output:
1
2
3
4
5
| 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
|
Step 3: Filter for Specific VMs
1
| esxcli --formatter=csv vm process list | sed 's/ /%/g' | sed 's/,/ /g' | grep vGPU
|
Example output:
1
2
| /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
|
1
| esxcli --formatter=csv vm process list | sed 's/ /%/g' | sed 's/,/ /g' | grep vGPU | awk '{print $6}'
|
Example output:
Step 5: Power Off VMs in a Loop
1
2
3
4
| 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
|
Additional Power Control Options
Power Off Types
When using esxcli vm process kill
, you can specify different types of shutdown:
1
2
3
4
5
6
7
8
| # Soft shutdown (graceful, like pressing the power button)
esxcli vm process kill --type=soft --world-id=<ID>
# Hard shutdown (immediate, like pulling the power cord)
esxcli vm process kill --type=hard --world-id=<ID>
# Force shutdown (last resort)
esxcli vm process kill --type=force --world-id=<ID>
|
Additional vim-cmd Power Operations
1
2
3
4
5
6
7
8
9
10
11
| # Power off a VM
vim-cmd vmsvc/power.off <vmid>
# Reset a VM
vim-cmd vmsvc/power.reset <vmid>
# Suspend a VM
vim-cmd vmsvc/power.suspend <vmid>
# Get power state of a VM
vim-cmd vmsvc/power.getstate <vmid>
|
Creating Reusable Scripts
For convenience, you can save these commands as shell scripts:
power-on-vgpu-vms.sh
1
2
3
4
5
6
| #!/bin/sh
for i in `vim-cmd vmsvc/getallvms | grep vGPU | awk '{print $1}' | sort -n`
do
echo "Powering on VM ID $i"
vim-cmd vmsvc/power.on $i
done
|
power-off-vgpu-vms.sh
1
2
3
4
5
6
| #!/bin/sh
for i in `esxcli --formatter=csv vm process list | sed 's/ /%/g' | sed 's/,/ /g' | grep vGPU | awk '{print $6}'`
do
echo "Powering off VM with World ID $i"
esxcli vm process kill --type=soft --world-id=$i
done
|