Tuesday, 4 August 2015

Vmware vSphere Esxi Guest VM Shutdown Script

Run the below script in vSphere Command Line to shutdown either Single Guest VM or All the Guest VM

#!/bin/bash
######### AUTHOR : Srinivasan ##############                               
######### Date   : 13.04.2015 ################                         
######### Purpose: To shutdown all the VM(s) in the Vsphere Esxi Host ##########

echo -n "Enter All to shutdown all VMs or Enter single to shutdown one VM:"
read choose
case $choose in

ALL) 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 = "poweredOn" ]; then
     echo " Going to shutdown VM with id $VM"
     #/sbin/vim-cmd vmsvc/power.shutdown $VM
     fi
     done
     echo "All VMs are Initiated for Shutdown"
;;

SINGLE)  echo "$(/sbin/vim-cmd vmsvc/getallvms)"
         echo -n "Enter the VM ID to shutdown:"
         read VM_ID
         VM_STATE=$(/sbin/vim-cmd vmsvc/get.summary $VM_ID | grep powerState | cut -d '=' -f 2 | sed -e 's/,//g' -e 's/"//g')
         if [ $VM_STATE = "poweredOn" ]; then
         echo "Going to shutdown VM with id $VM_ID"
         #/sbin/vim-cmd vmsvc/power.shutdown $VM
else
echo "Given VM_ID is not PoweredOn"
         fi
;;
*)      echo " Entered Something Else !! Please input correct parameter"
;;
esac


No comments:

Post a Comment