Powershell - OneLine useful # 1 - Re-synchronizing VM replication
This command will re-synchronize virtual machines replication, every 10 minutes.
It does not show errors and will stop after all synchronizations are ok
$hyperviseurs = @("SRV-HPV03","SRV-HPV04");$ErrorActionPreference = "SilentlyContinue";while($true){get-vm -ComputerName $hyperviseurs|Resume-VMReplication -Resynchronize;Start-Sleep -Seconds 5;get-vm -ComputerName $hyperviseurs|Reset-VMReplicationStatistics;$repl = get-vm -ComputerName $hyperviseurs|Get-VMReplication|Where-Object Health -notlike "Normal";if($repl.Count -eq 0){exit}else{$resync = $repl|Where-Object State -like "Resynchronizing";$errors = $repl - $resync;Write-Host -NoNewline "In re-synchronize " $resync.Count;Write-Host "";Write-Host -NoNewline "Errors " $errors.Count;Write-Host "";}Start-Sleep -Seconds 600}
Here the explanation :
# Array with all the hyper-v that you want to resynchronize
$hyperviseurs = @("SRV-HPV03","SRV-HPV04");
# Don't display errors
$ErrorActionPreference = "SilentlyContinue";
# Do infinite loop
while($true){
# Run resynchronization on all VM
get-vm -ComputerName $hyperviseurs|Resume-VMReplication -Resynchronize;
# Wait 5 seconds
Start-Sleep -Seconds 5;
# Reset replication statistics
get-vm -ComputerName $hyperviseurs|Reset-VMReplicationStatistics;
# Get the number of VM with different health than normal
$repl = get-vm -ComputerName $hyperviseurs|Get-VMReplication|Where-Object Health -notlike "Normal";
# If 0 vm with unnormal replication state
if($repl.Count -eq 0){
# Stop the script
exit
}
else{
# Get number of VM in resynchronizing state and not in normal Health
$resync = $repl|Where-Object State -like "Resynchronizing"
# Get the number of non resynchronizing state and not in normal Health
$errors = $repl - $resync
Write-Host -NoNewline "In re-synchronize " $resync.Count
Write-Host ""
Write-Host -NoNewline "Errors " $errors.Count
Write-Host ""
}
# Wait 10 minutes
Start-Sleep -Seconds 600;
}