Tag Archives: Move Replicas

Powershell Script to Move all Hyper-V VM Replicas to another Hyper-V Host

Powershell Script to Move all Hyper-V VM Replicas to another Hyper-V Host

Scenario:
Hyper-V Server 2012 R2 Cluster CLUSTERNAME.domain.local
Hyper-V Server 2012 R2 Replica Server server1.domain.local and server2.domain.local

This Script suspends the replica, moves the replica VM from server1.domain.local to server2.domain.local (D:\Hyper-V\), sets the new Replica Server and resumes replica for all replicas on server1.domain.local:

$ReplicaServerOld="server1.domain.local"
$ReplicaServerNew="server2.domain.local"
ForEach ($VMReplication in (Get-VMReplication -ComputerName (Get-Cluster -Name CLUSTERNAME.domain.local | Get-ClusterNode) | Where-Object {$_.ReplicaServer -like $ReplicaServerOld}))
{
	$VMPrimary = get-vm $VMReplication.Name -ComputerName $VMReplication.PrimaryServerName
	$VMReplica = get-vm $VMReplication.Name -ComputerName $VMReplication.ReplicaServerName
	$VMPrimary | Suspend-VMReplication
	Move-VM $VMReplica.Name -DestinationHost $ReplicaServerNew -IncludeStorage -DestinationStoragePath ("D:\Hyper-V\" + $VMReplica.Name)
	$VMPrimary | Set-VMReplication -ReplicaServerName $ReplicaServerNew
	$VMPrimary | Resume-VMReplication
}

You must configure Kerberos Constrained Delegation for Hyper-V Management or run the script on server1.domain.local
No error handling at the moment…