# encoding: ascii
# api: powershell
# title: New-LinkedClone
# description: PowerCLI script to create linked clones on an ESX server (does require vCenter). This feature is not normally supported on ESX, so this is a pretty nifty thing to do if you like living dangerously. Info on linked clones: http://www.vmware.com/support/ws55/doc/ws_clone_overview.html#wp1028798.
# version: 3.5
# type: script
# author: halr9000
# license: CC0
# x-poshcode-id: 1549
# x-archived: 2017-02-10T15:24:27
# x-published: 2010-12-19T18:12:00
#
#
#Requires -version 2
# TITLE: New-LinkedClone.ps1
# AUTHOR: Hal Rottenberg
# Adapted from a technique published originally by Keshav Attrey http://www.vmdev.info/?p=40
# Also see William Lam's Perl script: http://engineering.ucsb.edu/~duonglt/vmware/vGhettoLinkedClone.html
# And Leo's manual version for ESX 3.5: http://blog.core-it.com.au/?p=333
param (
[parameter(Mandatory=$true)][string]$SourceName,
[parameter(Mandatory=$true)][string]$CloneName
)
$vm = Get-VM $SourceName
# Create new snapshot for clone
$cloneSnap = $vm | New-Snapshot -Name "Clone Snapshot"
# Get managed object view
$vmView = $vm | Get-View
# Get folder managed object reference
$cloneFolder = $vmView.parent
# Build clone specification
$cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
$cloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot
# Make linked disk specification
$cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
$cloneSpec.Location.DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::createNewChildDiskBacking
# Create clone
$vmView.CloneVM( $cloneFolder, $cloneName, $cloneSpec )
# Write newly created VM to stdout as confirmation
Get-VM $cloneName