Enable Hyper-V Replica for all virtual machines in a specific Cloud

2 minute read

As discussed in my previous blog post I’m now working on a migration project to move virtual machines from one datacenter to another datacenter. I’m using Hyper-V Replica to replicate all the virtual machines with as less down-time as possible.

Hyper-V Replica can asynchronously replicate a selected VM running at a primary site to a designated replica site across LAN/WAN. The following schematic presents this concept – See more at: http://blogs.technet.com/b/yungchou/archive/2013/01/10/hyper-v-replica-explained.aspx#sthash.MI4qs7oi.dpuf

The figure below shows how I’m using HyperV replica at my current customer:

 

replica

 

The issue with Hyper-V Replica is that you need to configure the replication one by one in Hyper-V or in the Failover Cluster manager if you have clustered Hyper-V servers like me. However as Hyper-V Replica is a hyper-V feature you can use PowerShell to enable replication (more information on how to manage Hyper-V replica from Powershell can be found here ).

I have desinged a migration process for my customer so that I can migrate tenant per tenant (our cloud per cloud) to the new datacenter. To do this I need to be able to enable replication (and later planned Failover) per tenant or per cloud that are defined in VMM. Now that’s a challenge! 🙂

So I developed a PowerShell script that has the name of the Cloud I want to migrate as input and the name of the replica server to send the virtual machines. The script will:

  1. loop through all the VM’s in that cloud
  2. If replication is not enabled, enable replication
  3. connect to the hyperv host that is hosting the virtual machine at that moment
  4. launching a remote session to that host
  5. and start initial replication

The script has some variables in the beginning that you will have to change to reflect your own environment. Make sure you have configured replication before and that you have fully tested replication at least for one machine.You can download the whole script here and run it from your VMM server in PowerShell ISE.

[xml]

PowerShell Enable replica per VMM Cloud

Example usage:

Authhor: Alexandre Verkinderen

#region Variables and Arguments
#Define the Cloud that you would like to replicate. ALL VMs that are in that Cloud that are not yet replicated will be replicated to the HyperV Failover Replica Server.
$CloudName = ‘test-alex’
$ReplicaName = ‘ReplicaServerbroker.contoso.com’
$ReplicaPort = 80
$Auth = ‘Kerberos’
#endregion

#Getting all vms in the cloud
$VMs = @(Get-SCVirtualMachine | Where {$_.Cloud -like $CloudName})

#Loop through the vms and enable replication and start automatically the initial replication
Foreach ($vm in $VMs)
{
$VMname = $vm.Name
$VMhost = $vm.VMHost
If ($Vm.IsPrimaryVM -Match ‘False’)

{
Enable-VMReplication -VMName $vm.Name -ComputerName $VMhost -ReplicaServerName $ReplicaName -ReplicaServerPort $ReplicaPort -AuthenticationType $Auth -CompressionEnabled 1

Invoke-Command -computername $vm.VMHost -ScriptBlock{ param ($Name)
Start-VMInitialReplication -VMName $Name
} -Args $VMname
Write-Host ‘Replica enabled for= ‘ $VMname
}
Else
{
Write-Host ‘Replica already enabled for = ‘$VMname
}
}

[/xml]

The end-result will be that all my VM’s in a specific cloud are not being replicated to the other datacenter.  I just need to enable planned-failover to move them over but that will be for another weekend and another blogpost 🙂

Hope this helps,
Alexandre

Leave a comment