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"
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
Thread reported so moderators know it should be moved to the area for PowerCLI.
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
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
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.
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
Great Luc, it works OK, now I still have a request to modify the script to send a cloned VM to another ESXi
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