Workspace-Guru

New-VMLinkedClone

Jul 6th, 2018
6,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     A Scirpt to create a linked clone in VMware vSphere.
  4.      
  5. .DESCRIPTION
  6.     This script will take a snapshot of a Base VM (VM to becloned) and create new linked clone(s) from the snapshot. Linked clones use less storage since they only save the delta data. Instead of copying all data.
  7.     This script allows you to create multiple linked clones from one base snapshot. The script also checks if there isn't a snapshot for the linked clones alreday present on the BaseVM.
  8.  
  9. .NOTES
  10.     File Name  : New-VMLinkedClone.ps1
  11.     Author     : Chris Twiest - [email protected]
  12.     This script uses VMware PowerCLI make sure to install PowerCLI before running the script.
  13.  
  14. .LINK
  15.     https://workspace-guru.com
  16.  
  17. .FUNCTION PARAMETERS EXPLANATION
  18.     vCenterserver          ### Enter your vCenter Server
  19.     vCenterUser            ### Enter your vCenter Administrator Account
  20.     vCenterPassword        ### Enter your vCenter Administrator Password
  21.     BaseVM                 ### Enter the name of the machine you want to clone
  22.     TargetVMs              ### Enter the name of the clone, you can enter multiple targets for more clones like : "Fileserver01-Test","Fileserver02-Test"
  23.     ResourcePool           ### Enter the name of your resource pool, you can leave this empty if you use the VMhost parameter
  24.     TargetDatastore        ### Enter the name of the datastore on which the clone will be stored
  25.     VMHost                 ### Enter the VMHost, you can leave this empty if you use ResourcePool
  26.    
  27. .EXAMPLE
  28.     New-VMLinkedClone -vCenterserver "vcenter.domain.com" -vCenterUser "[email protected]" -vCenterPassword "P@ssw0rd" -BaseVM "FILESERVER01" -TargetVMs "FILESERVER01-ACC","FILTERSER01-TEST" -TargetDatastore "VMWare01" -VMHost "ESXI01.domain.com"
  29.    
  30.     This will create a snapshot named 'Linked-Snapshot-for-FILESERVER01-ACC FILTERSER01-TEST' on the VM FILESERVER01. From this snapshot there will be two VM's created called FILESERVER01-ACC FILTERSER01-TEST on ESXI host ESXI01 on Datastore VMware01.
  31.  
  32. .EXAMPLE
  33.     New-VMLinkedClone -vCenterserver "vcenter.domain.com" -vCenterUser "[email protected]" -vCenterPassword "P@ssw0rd" -BaseVM "SERVER01" -TargetVMs "SERVER02" -TargetDatastore "SSD" -ResourcePool "Production"
  34.    
  35.     This will create a snapshot named 'Linked-Snapshot-for-SERVER02' on the VM SERVER01. From this snapshot there will be a VM created called SERVER02 in ResourcePool Production on Datastore SSD.
  36.  
  37. #>
  38.  
  39. Function New-VMLinkedClone {
  40.     [CmdletBinding()]
  41.     param (
  42.         [Parameter(Mandatory = $True)]
  43.         [string]$vCenterserver,
  44.         [array] $TargetVMs,
  45.         [string]$vCenterUser,
  46.         [String]$vCenterPassword,
  47.         [string]$BaseVM,
  48.         [string]$TargetDatastore,
  49.         [Parameter(Mandatory = $false)]
  50.         [string]$ResourcePool,
  51.         [string]$VMHost
  52.     )
  53.  
  54.     #### Load in PowerCLI
  55.     # Returns the path (with trailing backslash) to the directory where PowerCLI is installed.
  56.     if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
  57.         if (Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI' ) {
  58.             $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'
  59.        
  60.         }
  61.         else {
  62.             $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware vSphere PowerCLI'
  63.         }
  64.         . (join-path -path (Get-ItemProperty  $Regkey).InstallPath -childpath 'Scripts\Initialize-PowerCLIEnvironment.ps1')
  65.     }
  66.     if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
  67.         Write-Host "VMware modules not loaded/unable to load"
  68.     }
  69.  
  70.     ### Connect vSphere
  71.     Connect-VIServer -server $vCenterserver -user $vCenterUser -Password $vCenterPassword
  72.  
  73.     ### Check if there is already a linkedclone snapshot for the clone and delete it
  74.     $SnapshotExists = Get-Snapshot -VM $BaseVM
  75.  
  76.     if ($SnapshotExists.Name -eq "Linked-Snapshot-for-$TargetVMs") {
  77.         Write-Host "Linked-Snapshot-for-$TargetVMs already exists" -ForegroundColor red
  78.         Read-Host -Prompt "Press any key to delete the snapshot and continue or CTRL+C to quit"
  79.  
  80.         $ExistingSnapshot = Get-Snapshot -VM $BaseVM -Name "Linked-Snapshot-for-$TargetVMs"
  81.         Remove-Snapshot -Snapshot $ExistingSnapshot -Confirm:$false
  82.         write-host "Old snapshot deleted" -ForegroundColor Green
  83.     }
  84.  
  85.     ### Create Master Snapshot
  86.     $SnapShot = New-Snapshot -VM $BaseVM -Name "Linked-Snapshot-for-$TargetVMs" -Description "Snapshot for linked clones for $TargetVM" -Memory -Quiesce
  87.     Write-Host "Snapshot create on $BaseVM" -ForegroundColor Green
  88.  
  89.     ### Create Linked Clones
  90.     ForEach ($TargetVM in $TargetVMs) {
  91.         if ($ResourcePool) {
  92.             $LinkedClone = New-VM -Name $TargetVM -VM $BaseVM -Datastore $TargetDatastore -ResourcePool $ResourcePool -LinkedClone -ReferenceSnapshot $SnapShot
  93.             write-host "Linked clone $TargetVM created" -ForegroundColor Green
  94.         }
  95.         if ($VMHost) {
  96.             $LinkedClone = New-VM -Name $TargetVM -VM $BaseVM -Datastore $TargetDatastore -VMHost $VMhost -LinkedClone -ReferenceSnapshot $SnapShot
  97.             write-host "Linked clone $TargetVM created" -ForegroundColor Green
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment