prasadsv
Enthusiast
Enthusiast

Script to Clone a VM with multiple disks attached from multiple datastores

Hi,

I had a VM which had disks attached from multiple datastores, say 8 datastores. I would like to have a PowerCLI script which clones the VM and have the clone of the VMDK's in its respective datastores like the Parent VM. i.e if the parent VM had 8 disks attched from 8 datastores,the cloned VM also should have 8 disks attached from 8 datastores. This can be achieved from vCenter UI after clicking on "Advanced" option for selecting Datastore. I would like have PowerCLI script for same.

Please help me in resolving this issue.

-Thanks,

Prasad S V

7 Replies
LucD
Leadership
Leadership

Have a look at Re: Clone VM with Multiple .VMDK


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

0 Kudos
prasadsv
Enthusiast
Enthusiast

Thanks for the quick Reply.

Here is the error that I encounter while running the script:

=============================

$vm.ExtensionData.CloneVM($folder.ExtensionData.MoRef, $targetName, $spec)

Exception calling "CloneVM" with "3" argument(s): "The operation is not

supported on the object."

At C:\VM_Clone_Multiple_DS_1.ps1:30 char:1

+ $vm.ExtensionData.CloneVM($folder.ExtensionData.MoRef, $targetName, $spec)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

Here is the script that I was using:

==========================

Connect-VIServer -Server $vc -User $vcUser -Password $vcPass

$sourceName = "cl_test"

$targetName = "cl_test11"

$targetDatastore = Get-Datastore tsse_clone_sh2

$folder = Get-Folder -Name cl_test

$vm = Get-VM -Name $sourceName

$spec = New-Object VMware.Vim.VirtualMachineCloneSpec

$spec.Template = $false

$spec.PowerOn = $false

$spec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec

$spec.Location.Datastore = $targetDatastore.ExtensionData.MoRef

$spec.Location.Pool = (Get-ResourcePool -Name Resources).ExtensionData.MoRef

Get-HardDisk -VM $vm | %{

    $disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator

    $disk.datastore = $_.ExtensionData.Backing.Datastore

    $disk.diskId = $_.ExtensionData.Key

    $spec.Location.Disk += $disk

}

$vm.ExtensionData.CloneVM($folder.ExtensionData.MoRef, $targetName, $spec)

Please let me know if I need to anything else in the script to make it run without error. Also,as suggested in the script,we are mentioning one Datastore information only but my requirement is to clone the disks to different datastores(in the same as its parent VM)

0 Kudos
LucD
Leadership
Leadership

Some things to check.

Do you have sufficient free space on the target datastore?

Does Get-ResourcePool -Name Resources only return 1 object?

What is the output of

Get-VM -Name cl_test | Get-HardDisk |

Select Name,

    @{N='DS';E={$_.ExtensionData.Backing.Datastore}},

    @{N='Id';E={$_.ExtensionData.Key}}


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

0 Kudos
LucD
Leadership
Leadership

Just did another test, and it works for me (PowerCLI 6.5.3 and vSphere 6.5U1).

Which versions are you on?

Can you give this one a try?

$vmName = 'TestVM'

$cloneName = "$($vmName)-clone"

if($vm = Get-VM -Name $cloneName){

    Remove-VM -VM $vm -DeletePermanently -Confirm:$false

}

$vm = Get-VM -Name $vmName

$targetDatastore = Get-Datastore -Name ($vm.ExtensionData.Config.Files.VmPathName.Split(']')[0].TrimStart('['))

$folder = $vm.Folder

$rPool = Get-ResourcePool -VM $vm

$spec = New-Object VMware.Vim.VirtualMachineCloneSpec

$spec.Template = $false

$spec.PowerOn = $false

$spec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec

$spec.Location.Datastore = $targetDatastore.ExtensionData.MoRef

$spec.Location.Pool = $rPool.ExtensionData.MoRef

Get-HardDisk -VM $vm | %{

    $disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator

    $disk.datastore = $_.ExtensionData.Backing.Datastore

    $disk.diskId = $_.ExtensionData.Key

    $spec.Location.Disk += $disk

}

$vm.ExtensionData.CloneVM($folder.ExtensionData.MoRef, $cloneName, $spec)


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

ffrancisco
Contributor
Contributor

Hi LucD - This script works very well.  just wondering if i could chose a different target DataCenter cluster or ESX host to clone the VM instead?  The idea is to clone the VM in my production cluster to my development cluster with multiple datastore targers when someone asks for a DB refresh.  thx in advance.

0 Kudos
LucD
Leadership
Leadership

In the VirtualMachineCloneSpec object, under the property Location, which is a VirtualMachineRelocateSpec object, there is a Host property.

On that property you can pass a MoRef to an ESXi node.


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

ffrancisco
Contributor
Contributor

thanks.  I found it last night too.  I've only done 5-6 tests because it takes an hour for each clone.  i'm still tweaking the scripts but it seems to be working well. 

appreciate your help on this.

-Franz

0 Kudos