自定义开机脚本
#/bin/bash
# author:jerry
# create date:2017-8-29
# last update:2020-5-27
status=`vim-cmd /hostsvc/hostsummary | grep inMaintenanceMode|cut -c 27`
[ $status == "t" ] && vim-cmd hostsvc/maintenance_mode_exit 1&> /dev/null
for on in `vim-cmd vmsvc/getallvms |awk 'NR>1{print $1}'`
do
if [ $on -eq 1 ]
then
continue
else
vim-cmd vmsvc/power.on $on
sleep 30
fi
done
自定义开机日志效果展示:
自定义关机脚本:
#!/bin/sh
# author: jerry
# create:2017-8-16
# update:2023-4-23
logs='/vmfs/volumes/datastore1/log.txt'
Vmid=`vim-cmd vmsvc/getallvms|awk 'NR>1{print $1}'`
for id in $Vmid ;do
timer=`date +%F_%T`
Vmstate=`vim-cmd vmsvc/power.getstate $id |tail -1|awk '{print $2}'`
if [ "$Vmstate" == "on" ] ;then
vim-cmd vmsvc/power.shutdown $id && echo "Vmid $id At $timer Closed Successed. " >> $logs
fi
done
sleep 180
off=`esxcli vm process list|grep World|awk '{print $3}'|wc -l`
if [ $off == 0 ] ;then
vim-cmd hostsvc/maintenance_mode_enter &> /dev/null
sleep 10
halt
else
Vmid=`vim-cmd vmsvc/getallvms|awk 'NR>1{print $1}'`
for id in $Vmid ;do
timer=`date +%F_%T`
Vmstate=`vim-cmd vmsvc/power.getstate $id |tail -1|awk '{print $2}'`
if [ "$Vmstate" == "on" ] ;then
vim-cmd vmsvc/power.off $id && echo "Vmid $id At $timer By Admin Force Closed Successed. " >> $logs
fi
done
sleep 120
off=`esxcli vm process list|grep World|awk '{print $3}'|wc -l`
if [ $off == 0 ] ;then
vim-cmd hostsvc/maintenance_mode_enter &> /dev/null
sleep 10
halt
else
VmPid=`ps |grep vmx|awk '{print $2}' |uniq`
for pid in $VmPid ;do
timer=`date +%F_%T`
kill $pid 1&> /dev/null && echo "Vmid Pid $pid killed By Admin At $timer. " >> $logs
done
vim-cmd hostsvc/maintenance_mode_enter &> /dev/null
sleep 10
halt
fi
fi
关机日志展示: