Workspace-Guru

CreateLinkedClone

Jul 4th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #### Parameters
  2. $vCenterserver = "vcenter.domain.com"                ### Enter your vCenter Server
  3. $vCenterUser = "Administrator@vsphere.local"         ### Enter your vCenter Administrator Account
  4. $vCenterPassword = "P@ssw0rd"                        ### Enter your vCenter Administrator Password
  5. $BaseVM = "FILESERVER01-PROD"                        ### Enter the name of the machine you want to clone
  6. $TargetVM = "FILSERVER01-TEST"                       ### Enter the name of the clone
  7. $ResourcePool = "ResourcePool01"                     ### Enter the name of your resource pool, if you dont use resources pools you need to changeline 32 with the esxi hostname, add -VMHost "your ESX server", remove -ResourcePool $ResourcePool.
  8. $TargetDatastore = "Datastore 01"                    ### Enter the name of the datastore on which the clone will be stored
  9.  
  10. #### Load in PowerCLI
  11. # Returns the path (with trailing backslash) to the directory where PowerCLI is installed.
  12. if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
  13.     if (Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI' ) {
  14.         $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'
  15.        
  16.     } else {
  17.         $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware vSphere PowerCLI'
  18.     }
  19.     . (join-path -path (Get-ItemProperty  $Regkey).InstallPath -childpath 'Scripts\Initialize-PowerCLIEnvironment.ps1')
  20. }
  21. if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
  22.     Write-Host "VMware modules not loaded/unable to load"
  23. }
  24.  
  25. ### Connect vSphere
  26. Connect-VIServer -server $vCenterserver -user $vCenterUser -Password $vCenterPassword
  27.  
  28. ### Check if there is already a linkedclone snapshot for the clone and delete it
  29. $SnapshotExists = Get-Snapshot -VM $BaseVM
  30.  
  31. if ($SnapshotExists.Name -eq "Linked-Snapshot-for-$TargetVM"){
  32.     Write-Host "Linked-Snapshot-for-$TargetVM already exists"
  33.     Read-Host -Prompt "Press any key to delete the snapshot and continue or CTRL+C to quit"
  34.  
  35.     $ExistingSnapshot = Get-Snapshot -VM $BaseVM -Name "Linked-Snapshot-for-$TargetVM"
  36.     Remove-Snapshot -Snapshot $ExistingSnapshot -Confirm:$false
  37.     }
  38.  
  39. ### Create Master Snapshot
  40. $SnapShot = New-Snapshot -VM $BaseVM -Name "Linked-Snapshot-for-$TargetVM" -Description "Snapshot for linked clones for $TargetVM" -Memory -Quiesce
  41.  
  42. ### Create Linked Clone
  43. $LinkedClone = New-VM -Name $TargetVM -VM $BaseVM -Datastore $TargetDatastore -ResourcePool $ResourcePool -LinkedClone -ReferenceSnapshot $SnapShot
Add Comment
Please, Sign In to add comment