Hey scripters! I recently had a task of migrating an entire virtual datacenter from one vCenter to another. It sounds like a pretty simple task, but manually recreating the cluster(s) could cause some human errors. I decided to write a script that would look at a specific vDatacenter and get all it’s clusters, then recreate that tree in the new vCenter. It’s pretty simple, really, it pulls all the clusters, then creates new clusters with the same attributes. When moving over the hosts, it first disconnects them from the original vCenter, then removes them from inventory.

I thought about disabling HA & DRS on the source vCenter cluster right before hand, but manually disconnecting & removing shouldn’t be an issue. If you add the host to the new vCenter without first removing from the old vCenter, that’s when you’ll get some HA weirdness because the old vCenter doesn’t know what just happened.

What does it do?

Simple. It looks that the vDatacenter name provided, gets all clusters & host in it. From there, it creates a new vDatacenter with the same name in the new vCenter, then recreates all the clusters. Here it actually uses the attributes of the original cluster when creating the new ones to get them as close as possible. Last, it adds the hosts back to their cluster, but in the new vCenter.

One nice thing is it also handles EVC mode.

If you have a central password repository, you could have the script pull from there, but I just had it prompt for username & password to add to the new vCenter.

What it doesn’t do.

  • Resource Pools
  • Permissions
  • DRS Rules & Groups
  • DRS Automated Migration Threshold
  • Or any other advanced cluster settings

It looks at the basic cluster settings that you can set via PowerCLI when creating a cluster. It does enable HA & DRS, but anything advanced, you’ll have to adjust.

I tried the resource pools thing, it’s kind of tricky, but hopefully I’ll get that going soon. I tried the same approach as the clusters, powershell/powercli complained about a few things, no matter how I did it.

Permissions should be easy, but will really depend on the new vCenter if all the roles & if everything’s set up identical.

DRS Rules & Groups shouldn’t be that hard, but I haven’t messed with it, and unfortunately, there’s no PowerCLI access to the migration threshold. You know, that little slider with “Conservative” and “Aggressive”, it just plops it right in the middle.

Where’s the script?

Right here (DOWNLOAD):

# Written by Luke Huckaba
# http://thephuck.com
# @thephuck
#
# v0.1 - 20140429
#
 
param([string]$vDC, [string]$srcvcenter, [string]$destvcenter)
if((-not($vDC)) -and (-not($vcenter))){Throw "You must supply a vDatacenter and vCenter: -vDC ###### -vcenter vcentername"}
 
$ErrorActionPreference = "SilentlyContinue"
 
Add-PSSnapin VMware.*
 
if($global:DefaultVIServer){Disconnect-VIServer -Server * -Force -Confirm:$false}
 
connect-viserver $srcvcenter
 
$vdatacenter = get-datacenter $vDC
 
$clusters = get-cluster -lo $vdatacenter
 
$VMhosts = get-vmhost -lo $vdatacenter
 
foreach($VMhost in $VMhosts){
     Get-VMHost $VMhost | Set-VMHost -State Disconnected | Remove-VMHost -Confirm:$false
}
 
Disconnect-VIServer -Server * -Force -Confirm:$false
connect-viserver $destvCenter
New-Datacenter $vDC -lo Datacenters
 
if($clusters){
    foreach($cluster in $clusters){
        New-Cluster -HARestartPriority $cluster.HARestartPriority -HAIsolationResponse $cluster.HAIsolationResponse -VMSwapfilePolicy $cluster.VMSwapfilePolicy -Name $cluster.Name -Location $vDatacenter.Name -HAEnabled -HAAdmissionControlEnabled -HAFailoverLevel $cluster.HAFailoverLevel -DrsEnabled -DrsMode $cluster.DrsMode -DrsAutomationLevel $cluster.DrsAutomationLevel -EVCMode $cluster.EVCMode
    }
}
 
Foreach($VMhost in $VMhosts){
 
    $esx_host_creds = $host.ui.PromptForCredential("ESXi Credentials Required", "Please enter credentials to log into the ESXi host.", "", "")
    if($clusters){
        Add-VMHost -Name $VMhost.Name -Location $VMhost.Parent -Credential $esx_host_creds -Force:$true
    }
    else{
        Add-VMHost -Name $VMhost.Name -Location $vdatacenter -Credential $esx_host_creds -Force:$true
    }
}

Have fun, and happy scripting!!!