Skip to content

Instantly share code, notes, and snippets.

@chrodriguez
Created July 30, 2018 15:54
Show Gist options
  • Save chrodriguez/f89b42f1a82907aeb5a2527f771cf82c to your computer and use it in GitHub Desktop.
Save chrodriguez/f89b42f1a82907aeb5a2527f771cf82c to your computer and use it in GitHub Desktop.
vmware vsphere set autostart script
#!/bin/bash
set -eo pipefail
usage() {
if [ ! -z "$1" ]; then
echo -e "ERROR:\n" 1>&2
echo -e "\t$@"
echo
fi
echo "Usage: $0 [-n machine-name-wildcard] [-t host] [-h] [-a]" 1>&2
exit 1;
}
while getopts ":n:t:ha" o; do
case "${o}" in
n)
MACHINE=${OPTARG}
;;
t)
HOST=${OPTARG}
;;
a) APPLY=true
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
HOST_FIND=""
VM_FIND=""
which govc > /dev/null || usage "govc is a required dependecy"
if [ ! -z "${HOST}" ]; then
HOST_FIND=(-name $HOST)
fi
if [ ! -z "${MACHINE}" ]; then
VM_FIND=(-name $MACHINE)
fi
TMPFILE=$(tempfile -p `basename $0`)
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for host in $(govc find / -type h ${HOST_FIND[@]} | sed 's/\r//'); do
govc host.autostart.info -host=$host > $TMPFILE
for vm in `govc find $host -type m ${VM_FIND[@]} | sed 's/\r//'`; do
CONTINUE=0
grep -q "^$vm" $TMPFILE || CONTINUE=1
if [ $CONTINUE -eq 1 ]; then
if [ ! -z "${APPLY}" ]; then
govc host.autostart.add -host="$host" "$vm"
else
echo $host:$vm
fi
fi
done
done
IFS=$SAVEIFS
rm $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment