Monday, June 5, 2017

Everyday Powershell - Part 43 - Automatically Resume Failed HyperV Replication

Everyday Powershell, as useful as HyperV Replication.

So on our 2012R2 HyperV boxes we occasionally get problems with replication. Usually after a Windows Update related reboot.


So we could go through each of the VMs failed Replications, right click, resume replication but what are we? Barbarians?!?

We've scheduled the following script  to run on our hypervisors;

$hypervisor = "someserver"

$vms = Get-VMReplication -ComputerName $hypervisor | where health -eq "critical" | where state -ne replicating | where name -ne "someservertoexclude"

foreach($vm in $vms){
    while((Get-VM -ComputerName $hypervisor -VMName $vm.name | where ReplicationState -ne "disabled" | get-vmReplication).state -ne "Replicating"){
        $vm.name
        (get-VM -ComputerName $hypervisor -VMName $vm.name | where ReplicationState -ne "disabled" | get-vmReplication).state
        if ((get-VM -ComputerName $hypervisor -VMName $vm.name | where ReplicationState -ne "disabled" | get-vmReplication).state -ne "Resynchronizing"){
            get-vm $vm.name -ComputerName $hypervisor | Resume-VMReplication -Resynchronize
            get-vm $vm.name -ComputerName $hypervisor | Resume-VMReplication -Continue
            get-vm $vm.name -ComputerName $hypervisor | Resume-VMReplication
            get-vm $vm.name -ComputerName $hypervisor | get-VMReplication
        }
        start-sleep -Seconds 10
    }
}

get-vm -ComputerName $hypervisor | where health -eq "warning" | foreach-object {Reset-VMReplicationStatistics $_.name}

So now when replication goes to hell because of a reboot, all the VMs just politley get back in line and do as they're told!