PoshCode Archive  Artifact [a6511a26b0]

Artifact a6511a26b08a01749d3ccaad4f224e4d1f649d2b48beb451c0ecbe6b47f0e48e:

  • File Newer-LinkedClone.ps1 — part of check-in [6b163ec490] at 2018-06-10 13:18:51 on branch trunk — Adapted from New-LinkedClone.ps1 from Hal Rottenberg. This version takes a third parameter called SnapName that allows a user to statically assign the snapshot that the clone should be built from. (user: Cameron Smith size: 1880)

# encoding: ascii
# api: powershell
# title: Newer-LinkedClone.ps1
# description: Adapted from New-LinkedClone.ps1 from Hal Rottenberg.  This version takes a third parameter called SnapName that allows a user to statically assign the snapshot that the clone should be built from.
# version: 3.5
# type: script
# author: Cameron Smith
# license: CC0
# x-poshcode-id: 3016
# x-archived: 2011-10-23T11:33:04
# x-published: 2011-10-19T17:45:00
#
#
#Requires -version 2

# TITLE: 	Newer-LinkedClone.ps1
# AUTHOR:	Cameron Smith (original by 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,
	[parameter(Mandatory=$true)][string]$SnapName
)
$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
$SnapRef = (Get-Snapshot -VM $SourceName -name "$SnapName") | Get-View
$cloneSpec.Snapshot = $SnapRef.moref

# 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