ESXi bash script to automate Power ON VM if its Powered OFF

This script with the help of crontab will enable ESXi 5.1 to poll the VM state every hour to check if it is ON and if not power it ON.

1. SSH to the host.
2. Copy and paste the following content to the shell script (auto-poweron.sh) which is placed in a vmfs volume.

Eg :-
Having enabled ssh access to your ESXi server, ssh in as root.
~ #vi /vmfs/volumes/datastore1/auto-poweron.sh

[crayon lang=”shell”]
vmname=”testvm”
vmid=$(vim-cmd vmsvc/getallvms | grep “$vmname” | awk ‘{print $1}’)
stat1=$(vim-cmd vmsvc/power.getstate “$vmid” | grep “off”)
if [ “$stat1” == “Powered off” ]
then
vim-cmd vmsvc/power.on “$vmid”
fi
[/crayon]

3. Replace ‘testvm’ with the appropriate vmName.
Continue reading