Starting and stopping vSphere remotely from the command line using SSH (physical power-on is manual or performed using iDRAC, iLo, etc.)

Starting / stopping of a vSphere environment over SSH can be a quick and easy way to manage a lab or test environment which needs to be powered off most of the time but used sometimes for testing.

Creating an SSH key

ssh-keygen -t rsa

Storing the public part of the SSH key on the ESXi hosts using SCP

scp ~/ENV/KEYS/mykey.pub root@esxi7n3:/etc/ssh/keys-root/authorized_keys

Finding out the vCenter ID

for i in {1..3}; do echo "esxi7n$i ##############"; sshi root@esxi7n$i "vim-cmd vmsvc/getallvms"; done | egrep "esx|vCenter" | awk {'print$1 $2 $3$4'}

Powering on the vCenter VM

sshi root@esxi7n1 "vim-cmd vmsvc/power.on 469"

Entering maintenance mode (with vSAN)

for i in {1..3}; do echo "esxi7n$i ##############"; sshi root@esxi7n$i "esxcli system maintenanceMode set -e true -m noAction &"; echo ""; done

Finding vCLS VMs

for i in {1..3}; do echo "esxi7n$i ##############"; sshi root@esxi7n$i "vim-cmd vmsvc/getallvms"; done | egrep "esx|vCLS" | awk {'print$1'}

Power off vCLS VM with id

sshi root@esxi7n2 "vim-cmd vmsvc/power.off 471"

Shut down ESXi hosts

for i in {1..3}; do echo "esxi7n$i ##############"; sshi root@esxi7n$i "esxcli system shutdown poweroff -r=\"Work done for today\""; echo ""; done