VMware Cloud Community
Vaclav_S
Contributor
Contributor
Jump to solution

Partial clone of virtual machine in VMware (via cmd New-VM)

Can you help me how to clone a VM, but I need to omit some disks (I need a script for regular cloning, I know that in the GUI there is an option via Customize VMs HW), I use this cmd in  PowerCli script:
New-VM -VM $VM1 -Name "$VM1-Clone" -VMHost $ESXi -datastore $Datastore -Notes "Clone $Time1" -Location "Clone"

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

To use the API, you could do something like this


@Vaclav_S wrote:

Can you help me how to clone a VM, but I need to omit some disks (I need a script for regular cloning, I know that in the GUI there is an option via Customize VMs HW), I use this cmd in  PowerCli script:
New-VM -VM $VM1 -Name "$VM1-Clone" -VMHost $ESXi -datastore $Datastore -Notes "Clone $Time1" -Location "Clone"


$vmsrc='SrcVM'
$vmClone = 'CloneVM'

$tgtDatastore = 'TgtDatastore'

# One or more HD to exclude
$removeHD = 'Hard disk 2', 'Hard disk 3'

$vm = Get-VM -Name $vmSrc
$hd = Get-HardDisk -VM $vm
$ds = Get-Datastore -Name $tgtDatastore

$spec = New-Object -TypeName VMware.Vim.VirtualMachineCloneSpec
$spec.Config = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$spec.Location = New-Object -TypeName VMware.Vim.VirtualMachineRelocateSpec
Get-HardDisk -VM $vm -PipelineVariable hd | ForEach-Object -Process {
    if($removeHD -contains $hd.Name){
        $delDisk = New-Object -TypeName VMware.Vim.VirtualDeviceConfigSpec
        $delDisk.Operation = 'remove'
        $delDisk.Device = $hd.ExtensionData
        $spec.Config.DeviceChange += $delDisk
    }
    else{
        $disk = New-Object -TypeName VMware.Vim.VirtualMachineRelocateSpecDiskLocator
        $disk.DiskBackingInfo = $hd.ExtensionData.Backing
        $disk.DiskId = $hd.ExtensionData.Key
        $disk.Datastore = $ds.Id
        $spec.Location.Disk += $disk
    }
}
$vm.ExtensionData.CloneVM($vm.ExtensionData.Parent, $vmClone, $spec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
8 Replies
scott28tt
VMware Employee
VMware Employee
Jump to solution

Thread reported so moderators know it should be moved to the area for PowerCLI.

 


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to use the New-VM cmdlet, you will have to use a Remove-Harddisk after the cloning completes.

If you want to do it in one call (like the Web Client), you will have to use the API.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To use the API, you could do something like this


@Vaclav_S wrote:

Can you help me how to clone a VM, but I need to omit some disks (I need a script for regular cloning, I know that in the GUI there is an option via Customize VMs HW), I use this cmd in  PowerCli script:
New-VM -VM $VM1 -Name "$VM1-Clone" -VMHost $ESXi -datastore $Datastore -Notes "Clone $Time1" -Location "Clone"


$vmsrc='SrcVM'
$vmClone = 'CloneVM'

$tgtDatastore = 'TgtDatastore'

# One or more HD to exclude
$removeHD = 'Hard disk 2', 'Hard disk 3'

$vm = Get-VM -Name $vmSrc
$hd = Get-HardDisk -VM $vm
$ds = Get-Datastore -Name $tgtDatastore

$spec = New-Object -TypeName VMware.Vim.VirtualMachineCloneSpec
$spec.Config = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$spec.Location = New-Object -TypeName VMware.Vim.VirtualMachineRelocateSpec
Get-HardDisk -VM $vm -PipelineVariable hd | ForEach-Object -Process {
    if($removeHD -contains $hd.Name){
        $delDisk = New-Object -TypeName VMware.Vim.VirtualDeviceConfigSpec
        $delDisk.Operation = 'remove'
        $delDisk.Device = $hd.ExtensionData
        $spec.Config.DeviceChange += $delDisk
    }
    else{
        $disk = New-Object -TypeName VMware.Vim.VirtualMachineRelocateSpecDiskLocator
        $disk.DiskBackingInfo = $hd.ExtensionData.Backing
        $disk.DiskId = $hd.ExtensionData.Key
        $disk.Datastore = $ds.Id
        $spec.Location.Disk += $disk
    }
}
$vm.ExtensionData.CloneVM($vm.ExtensionData.Parent, $vmClone, $spec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Vaclav_S
Contributor
Contributor
Jump to solution

Thanks Luc,

Does it remove as a postprocess? I don't have space on the target datastore to clone the whole VM and then delete the data disk.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not 100% sure how that method is implemented, but it looks in my tests that the excluded disk is not copied.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Vaclav_S
Contributor
Contributor
Jump to solution

Great Luc, it works OK, now I still have a request to modify the script to send a cloned VM to another ESXi

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In the VMware.Vim.VirtualMachineRelocateSpec object is a Host property where you can enter the MoRef (ID) of the target ESXi node. 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Vaclav_S
Contributor
Contributor
Jump to solution

Spoiler
thanks, but I can't debug the correct syntax, shouldn't you have an example?
Reply
0 Kudos