PoshCode Logo PowerShell Code Repository

New-LinkedClone by halr9000 6 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1549"></script>download | new post

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.

  1. #Requires -version 2
  2.  
  3. # TITLE:        New-LinkedClone.ps1
  4. # AUTHOR:       Hal Rottenberg
  5. # Adapted from a technique published originally by Keshav Attrey http://www.vmdev.info/?p=40
  6. # Also see William Lam's Perl script: http://engineering.ucsb.edu/~duonglt/vmware/vGhettoLinkedClone.html
  7. # And Leo's manual version for ESX 3.5: http://blog.core-it.com.au/?p=333
  8.  
  9. param (
  10.         [parameter(Mandatory=$true)][string]$SourceName,
  11.         [parameter(Mandatory=$true)][string]$CloneName
  12. )
  13. $vm = Get-VM $SourceName
  14.  
  15. # Create new snapshot for clone
  16. $cloneSnap = $vm | New-Snapshot -Name "Clone Snapshot"
  17.  
  18. # Get managed object view
  19. $vmView = $vm | Get-View
  20.  
  21. # Get folder managed object reference
  22. $cloneFolder = $vmView.parent
  23.  
  24. # Build clone specification
  25. $cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
  26. $cloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot
  27.  
  28. # Make linked disk specification
  29. $cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
  30. $cloneSpec.Location.DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::createNewChildDiskBacking
  31.  
  32. # Create clone
  33. $vmView.CloneVM( $cloneFolder, $cloneName, $cloneSpec )
  34.  
  35. # Write newly created VM to stdout as confirmation
  36. Get-VM $cloneName

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me