I have been searching for a simple solution to get the notification when something goes wrong with Hyper-V replica replication… The problem is that things can go realy bad if you have Hyper-V replica in critical state for long period of time (AVHD differential disk grows and can make a big mess on source server…)
So here is the PS script that checks replica health and sends mail if one or more of VMs have Warning or Critical replication state.
You can modify sending parameters by looking at examples @ http://petermorrissey.blogspot.com/2013/01/sending-smtp-emails-with-powershell.html
Just save the script as (for example) c:hvreplicamonhvreplicamon.ps1
Create a Task Scheduler taks (screenshots below)
if ((Get-VMReplication | select-string -inputobject {$_.Health} -pattern “Warning”) -like “Warning”)
{
$SMTPServer = “smtp.gmail.com”
$SMTPPort = “587”
$Username = “username@gmail.com”
$Password = “password”
$to = “my@emailwhereiwanttoreceivealert.com”
$subject = “Replica WARNING error on SERVERNAME”
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.to.add($to)
$message.from = $username
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
}
elseif ((Get-VMReplication | select-string -inputobject {$_.Health} -pattern “Critical”) -like “Critical”)
{
$SMTPServer = “smtp.gmail.com”
$SMTPPort = “587”
$Username = “username@gmail.com”
$Password = “password”
$to = “my@emailwhereiwanttoreceivealert.com”
$subject = “Replica CRITICAL error on SERVERNAME”
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.to.add($to)
$message.from = $username
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
}
Task Scheduler task parameters
Thank You! This script works great! Exactly what I was looking for.
LikeLike
where is the script?
LikeLike
Cheers, works perfectly – the sort of thing that MS should have had in the first place.
LikeLike
Always used this and it has worked really well with Server 2012, Server 2012 R2 and Server 2016, but for some reason it doesn’t work with Server 2019..?
Any ideas how we can get it working again…?
LikeLike
Hi! I will take a look in next days and I will update the info.
LikeLike
Thats great, thank you for your help, very much appreciated…
LikeLike