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 )