Skip to content

Instantly share code, notes, and snippets.

@AllPurposeBen
Created May 26, 2022 21:03
Show Gist options
  • Save AllPurposeBen/7d70124638d0dc9e39737555bbce6f55 to your computer and use it in GitHub Desktop.
Save AllPurposeBen/7d70124638d0dc9e39737555bbce6f55 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# To use this script, quit VMware Fusion first!!!
#
# You'll be dragging this script into Terminal, then dragging the *.vmx file into Terminal.
# This requires sudo rights, but you already knew that, right? :)
#
# /path/to/ThisScript.sh /path/to/MyVm.vmwarevm/MyVm.vmx
#
# 20180208 DM
# Arg handling function, version 21.4.0
scriptArgs=("${@}")
argParse () {
local index=0
local ret=1
local flag="$1"
local option="$2"
for arg in ${scriptArgs[@]}; do
((index+=1))
for subArg in $(echo "$flag"); do
if [[ "$arg" == "$subArg" ]]; then
#check for a value
value=${scriptArgs[@]:$index:1}
if [[ -n "$value" ]] && [[ "$value" != '-'* ]]; then
vals+=( "$value" )
fi
ret=0
fi
done
if [[ "$option" == '__single-match' ]] && [[ $ret -eq 0 ]]; then
break
fi
done
if [[ -n "$vals" ]]; then
printf '%s\n' "${vals[@]}"
fi
return $ret
}
# Help/Usage text
usage () {
cat << EOU
$(basename $0) Tool for configuring a VMWre VM for use with DEP workflows.
Required arguments:
--vm The path to the VM (.vmwarevm) you want to modify.
Eg. --vm '~/Desktop/TestVM.vmwarevm'
Optional arguments:
--serial The serial number you'd like to hard code into the VM.
Eg. --serial 'ABCDEF123456'
EOU
exit 2
}
if argParse '--help' || argParse '-h' || [[ -z "$1" ]]; then
usage
fi
## Get the path to the VM and thus VMX file
vmPath=$(argParse '--vm')
if [[ -z "$vmPath" ]]; then
echo "Missing required arg: --vm"
usage
elif [[ ! -d "$vmPath" ]]; then
echo "Can't find VM at path: $vmPath"
usage
else
VMXFILE=$(find "$vmPath" -name "*.vmx" | head -n 1)
if [[ -z "$VMXFILE" ]]; then
echo "No vmx file found in VM path: $vmPath/"
usage
else
# get the VM's name
vmName=$(grep ^'displayName =' "$VMXFILE" | awk -F '= ' '{print $NF}' | tr -d '"' )
fi
fi
## Get a serial number
SERIALNUMBER=$(argParse '--serial')
if [[ -z "$SERIALNUMBER" ]]; then
# No serial provided, randomize a new one
randomSerial=$(head -c 1024 /dev/urandom | base64 | tr -cd "[:upper:][:digit:]" | head -c 12)
echo "Creating random serial number..."
SERIALNUMBER="$randomSerial"
fi
## Get started
echo "Editing VM '$vmName', SN:$SERIALNUMBER"
## Remove device specific crud
sed -i '' '/ethernet0.addressType/d' "$VMXFILE"
sed -i '' '/ethernet0.generatedAddress/d' "$VMXFILE"
sed -i '' '/ethernet0.generatedAddressOffset/d' "$VMXFILE"
sed -i '' '/uuid.bios/d' "$VMXFILE"
sed -i '' '/uuid.location/d' "$VMXFILE"
sed -i '' '/hw.model/d' "$VMXFILE"
sed -i '' '/serialNumber/d' "$VMXFILE"
sed -i '' '/ethernet0.virtualDev/d' "$VMXFILE"
sed -i '' '/ulm.disableMitigations/d' "$VMXFILE"
sed -i '' '/ethernet0.startConnected/d' "$VMXFILE"
sed -i '' '/board-id.reflectHost/d' "$VMXFILE"
## Add Model Identifier and Serial Number
# Hard coded values
MODELIDENTIFIER='MacBookPro14,3'
BOARDID='Mac-551B86E5744E2388'
# Set values
echo "hw.model = \"$MODELIDENTIFIER\"" >> "$VMXFILE"
echo "serialNumber = \"$SERIALNUMBER\"" >> "$VMXFILE"
echo "board-id = \"$BOARDID\"" >> "$VMXFILE"
echo 'serialNumber.reflectHost = "FALSE"' >> "$VMXFILE"
echo 'hw.model.reflectHost = "FALSE"' >> "$VMXFILE"
echo 'smbios.reflectHost = "FALSE"' >> "$VMXFILE"
echo 'board-id.reflectHost = "FALSE"' >> "$VMXFILE"
echo 'ethernet0.virtualDev = "vmxnet3"' >> "$VMXFILE"
echo 'ulm.disableMitigations = "TRUE"' >> "$VMXFILE"
echo 'ethernet0.startConnected = "FALSE"' >> "$VMXFILE"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment