Last active
February 7, 2020 08:59
-
-
Save diginfo/bccab4077b7498eb8a6c00738bd316ea to your computer and use it in GitHub Desktop.
ESXI Clone All VMs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OVFTOOL="/scratch/vmware-ovftool/ovftool" | |
DOW=$(date +%a) | |
SRCVOL="SSG250G" | |
DESTVOL="DROBO/daily/$DOW/ovf" | |
clone() { | |
VMID=$(vim-cmd vmsvc/getallvms | grep $SRCVM | awk '{print $1}') | |
echo "CLONING $SRCVM ($VMID)" | |
ISTATE=$(vim-cmd vmsvc/power.getstate $VMID | sed -n 2p); | |
## POWER OFF | |
if [ "$ISTATE" == "Powered on" ]; then | |
echo "POWERING OFF $SRCVM..." | |
vim-cmd vmsvc/power.shutdown $VMID | |
vim-cmd vmsvc/power.off $VMID | |
## WAIT FOR POWEROFF | |
echo "WAITING FOR POWER OFF..." | |
until [ "$(vim-cmd vmsvc/power.getstate $VMID | sed -n 2p)" == "Powered off" ] | |
do | |
echo "WAITING..." | |
sleep 1; | |
done | |
fi | |
TGT="/vmfs/volumes/$DESTVOL/$SRCVM"; | |
SRC="/vmfs/volumes/$SRCVOL/$SRCVM/$SRCVM.vmx" | |
## Prepare Folders | |
if [ ! -d $TGT ]; then | |
mkdir -p $TGT; | |
else | |
rm -Rf $TGT/*; | |
fi | |
## Clone All to .OVF | |
$OVFTOOL $SRC "$TGT/$SRCVM.ovf" | |
## POWER UP IN ININTIAL | |
if [ "$ISTATE" == "Powered on" ]; then | |
echo "POWERING UP $SRCVM..." | |
vim-cmd vmsvc/power.on $VMID | |
fi | |
echo "$SRCVM DONE." | |
} | |
IDS=$(vim-cmd vmsvc/getallvms | grep ".vmx" | awk '{print $2}') | |
for VMID in $IDS ; do | |
SRCVM=$VMID | |
clone | |
done | |
echo "@@@@@ ALL-DONE @@@@@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment