Update 2/11/2013: A much easier method for doing this has been documented in this blog post. Thanks to reader Everett for the suggestion!
Update 2/7/13: A shell script that does what this post describes has been posted at github. Enjoy!
On a single ESXi 5.1 host (INCLUDING the free edition), I have been able to gracefully shutdown, poweroff or reboot the host and guest VMs using the commands documented below from the ESXi 5.1 shell.
You may want to do this in response to an uninterruptible power supply (UPS) power failure event trigger. In that case, you will need to install at least one guest VM (consider the VMware Virtual Management Appliance) that can run your UPS’ software or Linux’s Network UPS Tools (NUT). You do a USB or serial passthrough of your UPS (for locally connected ones). When the UPS software indicates a shutdown is required, you can run a shell script on the guest VM that SSH’s into the ESXi 5.1 host and then runs a shell script on the host or some variation of these commands directly.
For example: a UPS trigger fires on the VMA appliance, runs remote-shutdown.sh which does SSH into your host ESXi 5.1 as root (consider using key authentication so you don’t need to store the password) and runs the a shell script on the host (host-shutdown.sh) which you store on a datastore local to the host ESXi server.
Or you might just want to shut things down or do other maintenance via the shell/command line which these commands allow you to do.
The two command-line tools used here are vim-cmd and esxcli.
If you type vim-cmd, or vim-cmd <namespace> the tool has pretty good command-line help for figuring out what it can do – and that is quite a bit!
NOTE: I have not seen this method documented elsewhere and so you must assume this method is not officially supported by VMware – but it seems to work fine (and it may be able to be be improved on as well)!
1) list all vms
~ # vim-cmd vmsvc/getallvms
2) gracefully shutdown a vm (uses the VM’s “world id”) – you can also use power.off, power.reboot, power.suspend, etc.
~ # vim-cmd vmsvc/power.shutdown <VM/”world id” from step 1>
3) enter maintenance mode (immediately with no delay, this can only be done if ALL guest VMs have been shut down)
~ # esxcli system maintenanceMode set -e true -t 0
4) shutdown the ESXi host server
~ # esxcli system shutdown poweroff -d 10 -r “Shell initiated system shutdown”
5) try to exit maintenance mode real quick before shutdown!
~ # esxcli system maintenanceMode set -e false -t 0
If step # 5 does not succeed, your system will reboot in maintenance mode and you will have to manually take the system out of maintenance mode and restart your guest VMs.
These commands can be built into a simple shell script that you can then deploy on the ESXi host server itself. I have written one such script, and you can download it from GitHub.
There may be more information available on this VMware forums post (11/30/2012).