VMware vSphere: Create a linked clone with Powercli

Hi,

linked clones have some advantages:

  • New virtual machines are rapidly deployed. The initial task is only to reference to a snapshot of an existing virtual machine. No virtual disk file(s) will be copied at this time.
  • They save disk space. Because only the space difference to the origin snapshot is allocated.

I found no option in the vSphere respective the Webclient to create one, but PowerCli provides this. Lets do it.
Connect to vCenter

Connect-VIServer vcenter.subdomain.domain.local

Define the virtual machine which is the origin of the linked clone, and the Snapshotname on which the linked clone based.

$sOriginVM="mastervm"
$sOriginVMSnapshotName="mastervm_linkedclone_snap"


Name of the new virtual machine

$sNewVMName="linkedclonevm"

In which logical vSphere folder should the vm placed. In this example the same folder as origin VM.

$oVCenterFolder=(Get-VM $sOriginVM).Folder

Create a snapshot of the source VM

$oSnapShot=New-Snapshot -VM $sOriginVM -Name $sOriginVMSnapshotName -Description "Snapshot for linked clones" -Memory -Quiesce

Define the datastore where the linked clone will be stored

$oESXDatastore=Get-Datastore -Name "esxdatastore1"

Create the linked clone. For Windows system also Customisation makes sence(New SID, New Computername) these parameters are the same like a “normal” clone.

$oLinkedClone=New-VM -Name $sNewVMName -VM $sOriginVM -Location $oVCenterFolder  -Datastore $oESXDatastore -ResourcePool Resources -LinkedClone -ReferenceSnapshot $oSnapShot

Thats it. Start the VM

Start-VM $oLinkedClone

Make some things in the VM and stop it

Stop-VM $oLinkedClone -Confirm:$false

Delete the linked clone and remove the snapshot at the origin VM

Remove-VM -DeletePermanently $oLinkedClone -Confirm:$false
Remove-Snapshot -Snapshot $oSnapShot -Confirm:$false

Michael

11 thoughts on “VMware vSphere: Create a linked clone with Powercli”

  1. Hi, I’m getting a New-VM : 3/4/2015 5:00:38 PM New-VM The operation for the entity “XXXXX” failed with the following message: “The operation is not supported on the object.” That happens when I type in the long $oLinkedClone command. “XXXX” is the name of the VM I’m tried to make a link to. Any ideas?

      1. Hello Michael, I have multiple data storage in a cluster(SAN). It has 3 ESXi hosts: host1, host2,host3. I logged in as root on host1 and tried the steps here but I’m getting “The operation is not supported on the object.”

        1. Hi Singh,

          your script connects to a host not to vCenter?

          It’s difficult to troubleshoot such a problem without some details of the source VM. Can you send me the output of:

          (Get-VM YourVM).Harddisks | fl *

          Get-Snapshot -VM YourVM|fl *

          And the command line to create the linked clone. Is source VM powered off when you create the snapshot?

          Michael

  2. Michael,

    My script connects to vCenter.

    this is my (Get-VM YourVM).Harddisks | fl *

    StorageFormat : Thick
    Persistence : Persistent
    DiskType : Flat
    Filename : [In_iSCSI] workstation.dev.net/workstation.dev.n
    et-000002.vmdk
    CapacityKB : 62914560
    CapacityGB : 60
    ParentId : VirtualMachine-57
    Parent : workstation.dev.net
    Uid : /VIServer=XXXX@1.dev.net:443/VirtualMachine=Virtual
    Machine-57/HardDisk=2000/
    ConnectionState :
    ExtensionData : VMware.Vim.VirtualDisk
    Id : VirtualMachine-57/2000
    Name : Hard disk 1

    this is my Get-Snapshot -VM YourVM|fl *

    Description :
    Created : 3/6/2015 2:55:44 PM
    Quiesced : False
    PowerState : PoweredOn
    VM : workstation.dev.net
    VMId : VirtualMachine-57
    Parent :
    ParentSnapshotId :
    ParentSnapshot :
    Children : {03-06-2015}
    SizeMB : 4110.178806304931640625
    SizeGB : 4.0138464905321598052978515625
    IsCurrent : False
    IsReplaySupported : False
    ExtensionData : VMware.Vim.VirtualMachineSnapshotTree
    Id : VirtualMachineSnapshot-57-snapshot-1
    Name : 03-06-2015
    Uid : /VIServer=XXXX@workstation.dev.net:443/Snapshot=VirtualMach
    ineSnapshot-57-snapshot-1/

    My command to create linked clone is same command that you did here. Everything works except for this command:
    $oLinkedClone=New-VM -Name $sNewVMName -VM $sOriginVM -Location $oVCenterFolder -Datastore $oESXDatastore -ResourcePool Resources -LinkedClone -ReferenceSnapshot $oSnapShot

    My VM is powered on when I try to clone.

    -Singh

  3. This is very nice tutorial.
    If anyone is interesting in creating/deleting many linked clones from one “parent” I wrote a script. Search for Timi77/VMware-Linked-Clones on github.com (sorry no links allowed)

  4. Any thoughts on how to update that linked clone?

    Say I install a couple patches on the Master VM then want to update the link clone so it also has those patches but not have to loose the individual clone personality attributes.

Leave a Reply Cancel reply