VMware/Linked Clone Powershell Script

From Omnia
Revision as of 21:27, 26 October 2023 by Kenneth (talk | contribs) (→‎Clone-XP-SubPrime.ps1)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

New-LinkedClone.ps1

#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


Clone-XP.ps1

$i = 1

do {
.\New-LinkedClone.ps1 XP-Test XP-Clone1-$i
$i++
}
while ($i -le 49)

Clone-XP-SubPrime.ps1

Connect-VIServer -Server 192.168.2.4 -User fusion\administrator -Password [PASSWORD]

$i = 1

do {
.\New-LinkedClone.ps1 XP-Sub-Prime1 XP-Sub1-Clone$i
$i++
}
while ($i -le 24)

$i = 1

do {
.\New-LinkedClone.ps1 XP-Sub-Prime2 XP-Sub2-Clone$i
$i++
}
while ($i -le 25)


Disconnect-VIServer -Server * -Confirm:$False

keywords