-->
🏠 🔍
SHAREOLITE

VMware hypervisor commandline linux script to power ON OFF VM

VMware provides a bunch of software for managing the virtual machines hosted on a server. Some of these may be free to use but some are quick a penny thing. In some cases , it may be required to remotely manage a VM power status without using the GUI management Vclient software. In such cases the default basic Hypervisor command line comes handy.


 In this post , we discuss about one such topic through a linux shell script on HyperVisor basic commands using which one may get the virtual machine power status , Switch it ON or even power OFF

#!/bin/sh
# Shareolite test linux shell script - to Power ON VM / OFF a Vmware virtual machine

VM="$1"
echo "`date` - ShareoLite scripts - VMware VM start / stop"
status=`vim-cmd vmsvc/getallvms |grep "$VM"`                            # Command to get the list of VMs running on Hypervisor
if [ $? -eq 0 ]
then
vmid=`vim-cmd vmsvc/getallvms |grep $VM  |awk '{print $1}'`
vmname=`vim-cmd vmsvc/getallvms |grep $VM  |awk '{print $2}'`
echo "`date` - Virtual machine $vmname with VMID $vmid is present on this hypervisor host"
echo "`date` - Enter
1 - Power status check
2 - Power OFF VM
3 - Power ON VM
"
read entry
case $entry in
1)
echo "`date` - Checking power status of VM $vmname"
vim-cmd vmsvc/power.getstate $vmid                                     # Command to get the VM power status
;;
2)
echo "`date` - Powering off VM $vmname"
vim-cmd vmsvc/power.off $vmid                                              # Command Power OFF VM a VM based on VMID
;;
3)
echo "`date` - Powering ON  VM $vmname"
vim-cmd vmsvc/power.on $vmid                                             # Command to Power ON VM a VM based on VMID
;;
*)
echo "`date` - Wrong option"
;;
esac
else
echo "`date` - Virtual machine $VM does not exist on this hypervisor"
fi

Sample commands output :

~ # ./script.sh TESTVM1      # Pass the argument as VM name here 
Mon Sep  1 13:07:24 UTC 2014 - ShareoLite scripts - VMware VM start / stop
Mon Sep  1 13:07:26 UTC 2014 - Virtual machine TESTVM11_SLite_ESX with VMID 1 is present on this hypervisor host
Mon Sep  1 13:07:26 UTC 2014 - Enter
1 - Power status check
2 - Power OFF VM
3 - Power ON VM

1
Mon Sep  1 13:07:27 UTC 2014 - Checking power status of VM TESTVM11_SLite_ESX
Retrieved runtime info
Powered on

~ # ./test.sh TESTVM1
Mon Sep  1 13:07:31 UTC 2014 - ShareoLite scripts - VMware VM start / stop
Mon Sep  1 13:07:33 UTC 2014 - Virtual machine TESTVM11_SLite_ESX with VMID 1 is present on this hypervisor host
Mon Sep  1 13:07:33 UTC 2014 - Enter
1 - Power status check
2 - Power OFF VM
3 - Power ON VM

2
Mon Sep  1 13:07:34 UTC 2014 - Powering off VM TESTVM11_SLite_ESX
Powering off VM

~ # ./test.sh TESTVM1
Mon Sep  1 13:08:14 UTC 2014 - ShareoLite scripts - VMware VM start / stop
Mon Sep  1 13:08:16 UTC 2014 - Virtual machine TESTVM11_SLite_ESX with VMID 1 is present on this hypervisor host
Mon Sep  1 13:08:16 UTC 2014 - Enter
1 - Power status check
2 - Power OFF VM
3 - Power ON VM

1
Mon Sep  1 13:08:25 UTC 2014 - Checking power status of VM TESTVM11_SLite_ESX
Retrieved runtime info
Powered on


Hope this helps to some Hyper-visor command line geeks.
Comments

–>