43 lines
1.1 KiB
Bash
43 lines
1.1 KiB
Bash
#/bin/sh
|
|
|
|
echo "Putting the Host into maintenance mode ..."
|
|
#vim-cmd hostsvc/maintenance_mode_enter
|
|
esxcli system maintenanceMode set -e true -t 0 &
|
|
|
|
VMS=`vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'`
|
|
for VM in $VMS; do
|
|
if [ $VM -eq $VM ]; then
|
|
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
|
|
if [ "$PWR" == "Powered on" ] ; then
|
|
name=`vim-cmd vmsvc/get.config $VM | grep -i "name =" | awk '{print $3}' | head -1 | cut -d "\"" -f2`
|
|
echo "Shutting down: $name"
|
|
vim-cmd vmsvc/power.shutdown $VM > /dev/null &
|
|
fi
|
|
fi
|
|
done
|
|
|
|
echo "Waiting for all VM's to be powered off ..."
|
|
while true ; do
|
|
RUNNING=0
|
|
for VM in $VMS; do
|
|
if [ $VM -eq $VM ]; then
|
|
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
|
|
if [ "$PWR" == "Powered on" ] ; then
|
|
RUNNING=1
|
|
fi
|
|
fi
|
|
done
|
|
if [ $RUNNING -eq 0 ] ; then
|
|
echo "DONE"
|
|
break
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
echo "Shutting down the Host in 10secs ..."
|
|
esxcli system shutdown poweroff -d 10 -r "Power-Monitor script initiated system shutdown"
|
|
|
|
echo "Disable maintenance mode before powering off ..."
|
|
esxcli system maintenanceMode set -e false -t 0
|
|
|
|
exit 0 |