Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### Parameters
- $vCenterserver = "vcenter.domain.com" ### Enter your vCenter Server
- $vCenterUser = "Administrator@vsphere.local" ### Enter your vCenter Administrator Account
- $vCenterPassword = "P@ssw0rd" ### Enter your vCenter Administrator Password
- $BaseVM = "FILESERVER01-PROD" ### Enter the name of the machine you want to clone
- $TargetVM = "FILSERVER01-TEST" ### Enter the name of the clone
- $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.
- $TargetDatastore = "Datastore 01" ### Enter the name of the datastore on which the clone will be stored
- #### Load in PowerCLI
- # Returns the path (with trailing backslash) to the directory where PowerCLI is installed.
- if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
- if (Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI' ) {
- $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'
- } else {
- $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware vSphere PowerCLI'
- }
- . (join-path -path (Get-ItemProperty $Regkey).InstallPath -childpath 'Scripts\Initialize-PowerCLIEnvironment.ps1')
- }
- if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
- Write-Host "VMware modules not loaded/unable to load"
- }
- ### Connect vSphere
- Connect-VIServer -server $vCenterserver -user $vCenterUser -Password $vCenterPassword
- ### Check if there is already a linkedclone snapshot for the clone and delete it
- $SnapshotExists = Get-Snapshot -VM $BaseVM
- if ($SnapshotExists.Name -eq "Linked-Snapshot-for-$TargetVM"){
- Write-Host "Linked-Snapshot-for-$TargetVM already exists"
- Read-Host -Prompt "Press any key to delete the snapshot and continue or CTRL+C to quit"
- $ExistingSnapshot = Get-Snapshot -VM $BaseVM -Name "Linked-Snapshot-for-$TargetVM"
- Remove-Snapshot -Snapshot $ExistingSnapshot -Confirm:$false
- }
- ### Create Master Snapshot
- $SnapShot = New-Snapshot -VM $BaseVM -Name "Linked-Snapshot-for-$TargetVM" -Description "Snapshot for linked clones for $TargetVM" -Memory -Quiesce
- ### Create Linked Clone
- $LinkedClone = New-VM -Name $TargetVM -VM $BaseVM -Datastore $TargetDatastore -ResourcePool $ResourcePool -LinkedClone -ReferenceSnapshot $SnapShot
Add Comment
Please, Sign In to add comment