PowerCLI: Migrate DRS VM Group Members

When ever you need to migrate to from one vCenter instance to another there are lots of things you need to migrate.

This PowerCLI script will help you migrate members from one DRS VM group to another. It can easily be modified to be part of a bigger context, or migrate all groups available.

Prerequisites

The prerequisites are that you have both the new and the old vCenter running, and that you have disconnected you hosts in the old vCenter and connected them to the new one. Do NOT remove them from you old vCenter. Leave them disconnected.

Please read and understand the script before running it. Also this is provided as is, and used at your own risk.

The Script

# Migrate DRS-Group-Members
Import-Module VMware.PowerCLI

$global:DefaultVIServers | Disconnect-VIServer -Confirm:$false

$srcVc = "fqdn to source vCenter"
$dstVc = "fqdn to destination vCenter"

Connect-VIServer $srcVc
Connect-VIServer $dstVc

$srcDrsGroupName = "<Source DRS Group Name"
$dstDrsGroupName = "<Destination DRS Group Name"

$srcDrsGroup = Get-DrsClusterGroup $srcDrsGroupName -Server $srcVc
$dstDrsGroup = Get-DrsClusterGroup $dstDrsGroupName -Server $dstVc

$VMs = $srcDrsGroup.member

$x = 0 
$max = $VMs.Count
foreach ($vm in $VMs | Select-Object -Skip $x) {
    $x += 1
    $dstVM = Get-VM -Server $dstVc $vm.Name
    if (-not $dstVM) {
        throw "Could not find VM: $vm"
    }
    Write-Host "($($x)/$($max)) Adding VM: $dstVM to group: $dstDrsGroupName"
    $null = $dstDrsGroup | Set-DrsClusterGroup -Add -VM $dstVM
}

Hope that you found it useful. Please leave a comment if you did.

Related Post