Skip to content

Instantly share code, notes, and snippets.

@lamw
Created August 6, 2025 00:49
Show Gist options
  • Save lamw/6dddf7bd3ed05d83ccaaf0520433a403 to your computer and use it in GitHub Desktop.
Save lamw/6dddf7bd3ed05d83ccaaf0520433a403 to your computer and use it in GitHub Desktop.
Rename vSAN Virtual Machine (name) along with its configuration files
#!/bin/ash
# Thank you ChatGPT for creating the script given my requirement/prompts
# Usage: ./rename-esxi-vm.sh old_vm_name new_vm_name [--apply]
#
# Example dry-run (default):
./rename-esxi-vm.sh photon-03 photon-02
# Apply changes:
./rename-esxi-vm.sh photon-03 photon-02 --apply
OLD="$1"
NEW="$2"
MODE="$3"
if [ -z "$OLD" ] || [ -z "$NEW" ]; then
echo -e "\nUsage: "
echo -e "\t$0 old_vm_name new_vm_name [--apply]\n"
exit 1
fi
# Default is dry-run unless --apply is explicitly set
if [ "$MODE" = "--apply" ]; then
APPLY=1
echo "🚨 APPLY mode enabled β€” changes will be made!"
else
APPLY=0
echo "πŸ§ͺ Dry-run mode (default): no files will be modified"
fi
echo "Renaming VM from '$OLD' to '$NEW'..."
# Step 1: Rename all non-VMDK/non-VMX files
for file in ${OLD}.*; do
ext="${file##*.}"
case "$ext" in
vmdk|vmx) continue ;; # Skip VMDK and VMX
esac
newfile="${file/$OLD/$NEW}"
echo "πŸ“ Rename: $file β†’ $newfile"
[ "$APPLY" -eq 1 ] && mv "$file" "$newfile"
done
# Step 2: Rename VMDK files using vmkfstools
for vmdk in ${OLD}*.vmdk; do
[ ! -f "$vmdk" ] && continue
suffix="${vmdk#$OLD}"
newvmdk="${NEW}${suffix}"
echo "πŸ’½ vmkfstools -E \"$vmdk\" \"$newvmdk\""
[ "$APPLY" -eq 1 ] && vmkfstools -E "$vmdk" "$newvmdk"
done
# Step 3: Rename .vmx and update its contents
VMX_FILE="${OLD}.vmx"
NEW_VMX_FILE="${NEW}.vmx"
if [ -f "$VMX_FILE" ]; then
echo "πŸ“„ Rename .vmx: $VMX_FILE β†’ $NEW_VMX_FILE"
if [ "$APPLY" -eq 1 ]; then
cp "$VMX_FILE" "$NEW_VMX_FILE"
sed -i "s/$OLD/$NEW/g" "$NEW_VMX_FILE"
rm -f "$VMX_FILE"
else
echo "πŸ“ Would copy and update internal references in $NEW_VMX_FILE"
fi
else
echo "⚠️ .vmx file not found: $VMX_FILE"
fi
echo
if [ "$APPLY" -eq 1 ]; then
echo "βœ… Rename completed (applied)"
else
echo "βœ… Dry-run completed β€” use '--apply' to execute changes"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment