Tuesday, 4 August 2015

Vmware vSphere Esxi Host Shutdown Script

Run the below Script in vSphere Command Line.

#!bin/bash
######### AUTHOR : Srinivasan ##############                               
######### Date   : 13.04.2015 ##############                               
######### Purpose: To shutdown the Vsphere Esxi Host ##########
echo -e "Are you sure you want to Shutdown Vsphere Esxi Host(yes/no):"
read state
if [ $state = "yes" ]; then
   count=0
   for VM in $(/sbin/vim-cmd vmsvc/getallvms | cut -d ' ' -f 1 | tail -n +2)
    do
   VM_STATE=$(/sbin/vim-cmd vmsvc/get.summary $VM | grep powerState | cut -d '=' -f 2 | sed -e 's/,//g' -e 's/"//g')
      if [ $VM_STATE = "poweredOff" ]; then
      count=$((count+1))
      fi
    done
   N=`/sbin/vim-cmd vmsvc/getallvms | cut -d ' ' -f 1 | tail -n +2 | wc -l`
   if [ $N == $count ]; then
   echo -e "\t Going to Shutdown Vsphere Host"
   /sbin/poweroff
   else
   echo -e "\t Please Shutdown All the Guest VM(s) before shutting down Vsphere Host"
   fi
else
echo -e "Please ensure before going for shutdown"
fi


No comments:

Post a Comment