VMware Cloud Community
steveschofield
Enthusiast
Enthusiast

Verify linked clone script and placing on another datastore

I'm implementing a self-service portal creating linked clones test boxes.  The script below works and places new machines on another datastore besides the one the ParentVM lives on.  Just to confirm, I'm curious items marked in red are both required?  From my testing, if the #1 line is not there, it creates a full clone.  Can someone pls verify?

############################

#Write-Host "Loading Snap-in"

############################

function LoadSnapin{

  param($PSSnapinName)

  if (!(Get-PSSnapin | where {$_.Name -eq $PSSnapinName})){

    Add-pssnapin -name $PSSnapinName

  }

}

LoadSnapin -PSSnapinName   "VMware.VimAutomation.Core"

#Variables

$cloneName="steveSchofield"

$vCenterHostName="vCenter"

$vCenterUserName="domainUser"

$vCenterPassword="xxxxx"

$ParentVMName = "win2012R2std"

$DataStore="DS1"

#connect to vSphere

Connect-viserver -Server $vCenterHostName -User $vCenterUserName -Password $vCenterPassword

#Creates the VM

$sourceVM = Get-VM $ParentVMName | Get-View

$cloneFolder = $sourceVM.parent

$cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec

$cloneSpec.Snapshot = $sourceVM.Snapshot.CurrentSnapshot

$cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec

#1 #Needed for linked clone otherwise it'll be a full clone

$cloneSpec.Location.DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::createNewChildDiskBacking

#2 #Places on another datastore with link back to parent VM

$cloneSpec.Location.Datastore = (Get-View -ViewType Datastore -Property Name -Filter @{"Name"=$DataStore}).MoRef

#Creates VM

$sourceVM.CloneVM_Task( $cloneFolder, $cloneName, $cloneSpec )

0 Kudos
1 Reply
LucD
Leadership
Leadership

Both your assumptions are correct, as confirmed by the API Reference.

#1 : in VirtualMachineRelocateSpec it says, under the diskMoveType property, "If left unset then moveAllDiskBackingsAndDisallowSharing is assumed.". And that indeed creates a full clone.

#2 : in VirtualMachineRelocateSpec it says "The datastore where the virtual machine should be located".


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos