VMware Cloud Community
praveenp
Contributor
Contributor

Looking for script that can move VMs to specific Resource Pools based on Resource pool structure from old vcenter

Hi All,

I need to migrate 1700+ VMs from 6 ESXi hosts to a new vCenter [6.5U2b]. The old infrastructure is on 6.0U2. I was able to export the Resource Pool structure successfully by referring to "Export and Import of Resource Pools using PowerCLI ". I have imported the resource pool to new vcenter and it works perfect [thanks a lot to Luc!!] .

My resource pool tree sample would like, where BNG, Gold, Tier# are resource pools and virtual machines can be inside any of these resource pools.

BNG

     Gold

          Tier1

               Virtual machine 1

     Silver

          Tier1

     Bronze

          Tier1

APAC

     Gold

          Tier1

               Virtual machine 1

     Silver

          Tier1

     Bronze

          Tier1

I have checked couple of scripts available, however those failed in my case as resource pool names become same under multiple parent pools. Is there any way that we can properly export VM structure and then move the VMs to respective pool ?

Note : I went through another article which I got online, Virtual Admin: vCenter Migration , I tried scripts under step 5 and 12. However, I get issue when the child pool names are same under multiple parent pools.

Can anyone help me here ?

Thanks and Regards,

Praveen

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership

Try something like this.

You might have to add some extra parameters on the Move-VM cmdlet.

function Get-RPPath{

    [cmdletbinding()]

    param(

    [parameter(ValueFromPipeline)]

    [VMware.VimAutomation.ViCore.Types.V1.Inventory.ResourcePool]$ResourcePool,

    [VMware.VimAutomation.ViCore.Types.V1.VIServer]$Server

    )

    Begin{

        $root = Get-Folder -Name Datacenters -Server $Server

    }

    Process{

        $path = @($ResourcePool.Name)

        $parent = Get-View -Id $resourcePool.ExtensionData.Parent -Property Name,Parent -Server $Server

        while($parent.MoRef -ne $root.ExtensionData.MoRef){

            if('host','vm' -notcontains $parent.Name){

                $path += $parent.Name

            }

            $parent = Get-View -Id $parent.Parent -Server $Server

        }

        [array]::Reverse($path)

        $path -join '\'

    }

}

function Get-RPFromPath{

    [cmdletbinding()]

    param(

    [parameter(ValueFromPipeline)]

    [string]$Path,

    [VMware.VimAutomation.ViCore.Types.V1.VIServer]$Server

    )

    Begin{

        $root = Get-Folder -Name Datacenters -Server $Server

    }

    Process{

        $obj = $root

        $Path.Split('\') | ForEach-Object -Process {

            $obj = Get-Inventory -Name $_ -Location $obj -Server $Server

        }

        $obj

    }

}

$vc1 = Connect-VIServer -Server vc1

$vc2 = Connect-VIServer -Server vc2

foreach($rp in Get-ResourcePool -Server $vc1){

    $newRP = $rp | Get-RPPath -Server $vc1 | Get-RPFromPath -Server $vc2

    Get-VM -Location $rp | Move-VM -Destination $newRP -Confirm:$false

}


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

Reply
0 Kudos