VMware Cloud Community
GreyhoundHH
Enthusiast
Enthusiast

svMotion Config-File & first VMDK to new datastore

Hi,

as we're currently having to move all VMs of numerous datastores to new datastores I'm looking for a way to automate it.

Our VMs may consist of multiple VMDKs but only the first needs to be moved with the config-file.

So is there already some script to move all VMs (only Config & first VMDK) on a given datastore to a new datastore?

Benefit would be changing the VMDK to thick-eager-zeroed with the move.

Kind regards

0 Kudos
1 Reply
LucD
Leadership
Leadership

You will have to revert to the RelocateVM method.

Try something like this

It will move the VM to the target datastore, that will take care of the Configuration files.

Then per harddisk it determine the target datastore.

For the 1st harddisk, the new datastore, for all others the datastore they are on.

And it will convert the 1st harddisk to Thick

$vm = Get-VM -Name MyVM

$destinationdsHD1 = Get-Datastore -Name MyTargetDS

$HardDisks = @(Get-HardDisk -VM $vm)

$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec

$spec.Datastore = $destinationds.Extensiondata.MoRef

1..$HardDisks.Count | %{

    $objDisk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator

    if($_.Name -eq 'Hard disk 1'){

        $newFilename = "[" + $destinationds.Name + "]" + ($HardDisks[$_ -1].Filename.Split(']')[1])

        $objBackinginfo = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

        $objBackinginfo.DiskMode = "persistent"

        $objBackinginfo.eagerlyScrub = $true

        $objBackinginfo.FileName = $newFilename

       

        $objDisk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator

        $objDisk.DiskID = $HardDisks[$_ - 1].Id.Split('/')[1]

        $objDisk.DataStore = $destinationds.Extensiondata.MoRef

        $objDisk.diskBackingInfo = $objBackinginfo   

    }

    else{

        $objDisk.diskBackinginfo = $_.ExtensionData.Backing

        $objDisk.DiskId = $HardDisks[$_ - 1].Id.Split('/')[1]

        $objDisk.DataStore = $_.Extensiondata.Backing.Datastore

    }

    $spec.Disk += $objDisk       

}

$vm.ExtensionData.RelocateVM_Task($spec, "defaultPriority")


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

0 Kudos