PowerCLI Script to Clone new ESXi VMs from Templates without vCenter

PowerCLI Script to Clone new ESXi VMs from Templates without vCenter

One of the very useful, and time saving, features of vCenter is its templating and cloning mechanism whereby you can take an OS image, convert it to a template and then quickly create new VMs from this. When using the free ESXi product without vCenter, this is not available so to create a new VM from an existing VM can be a slow and cumbersome process.

As I run a home lab on ESXi without vCenter, I decided to write a PowerCLI script, complete with a graphical user interface, to make creating new VMs much easier and quicker.

The templates are just VMs which have previously been built and in my case, SysPrep'd as they are Windows machines so they all have "Sysprep" in the VM name.

Once the script is launched and an ESXi host connected to, where it will prompt for credentials if none are saved, supplied via the -username and -password options or set in %esxiusername% and %esxipassword% environment variables, enter a pattern (regex) for your templates and click "Fetch" to populate the list where you can select the required template.

esxi cloner gui.PNG

ESXi supports linked disks which means that a new VM can be created very quickly by basing it on an existing snapshot of another VM so that the new VM's disk is a differencing/delta disk of the parent rather than having to copy the template VM's hard disks. Simply tick the "Linked Clone Disks" box and then select a snapshot from the list.

When "OK" is pressed, the data entered will be sanity checked and if all appears good then the cloning will commence whereby a new VM with all the same virtual hardware devices as the template will be created and then copies of disks created and attached to the VM, optionally starting it once creation has finished.

It will also create multiple VMs where you must put "%d" in the New VM Name field which will be replaced by the number of each new VM as it is created.

The script uses PowerCLI which must be installed before the script is run.

There's a video here showing it in action and the script can be downloaded here.

Whilst every effort has been made to ensure that this script causes no unintended changes to your VMware environment, the author cannot accept any liability and you use the script entirely at your own risk. Please see the note below about protecting template disks from deletion.

Script Parameters

The script can be invoked without any parameters, where the ESXi host and template pattern will be retrieved from the registry if they have previously been saved. It can also be invoked with parameters to pre-populate some of the GUI fields to save time. For example:

& '.\ESXi Cloner.ps1' -linkedClone -powerOn -dataStore SSD

which will tick the boxes for linked clones and powering on after creation and set the datastore to the one called "SSD" in the user interface.

The script can also be used without the user interface to create one or move VMs. For example:

& '.\ESXi Cloner.ps1' -esxihost 192.168.0.48 -templateName "Server 2016 Sysprepd" -dataStore SSD -vmName "Manuel %d" -snapshot "For linked clone" -noGui -notes "Created for fun" -numCpus 1 -MB 1024 -count 2 -Verbose

Where the number of CPUs and memory configuration in the template VM "Server 2016 Sysprepd" is being overridden.

Full PowerShell native help is included within the script.

Protecting Parent Disks

When linked clones are used, care must be taken to protect the parent disk, in the template VM, from being deleted when the cloned VM is deleted as this is the default behaviour in VMware. There are two ways to avoid this deletion:

  1. Unregister rather than delete linked clone VMs and then delete its files manually, e.g. through the datastore browser
  2. Edit the parent disk to mark it as undeletable by adding the following line to each vmdk (not the -flat* binary files):

ddb.deletable = "false"

So that a vmdk file might look like this:

pastedImage_4.png

Version history
Revision #:
1 of 1
Last update:
‎11-05-2018 05:12 AM
Updated by: