Here is an example Hyper-V replica email notification script.
Scenario is Hyper-V cluster replicating business critical VMs to a single Hyper-V host. The script can be ran as a scheduled task on the DR Hyper-V host to send email notifications. The account running the script must have Hyper-V permissions.
Script:
# Created by Chris Hayward # Purpose - To send a daily email displaying Hyper-V Replica status # Version 1.0 # Get date in UK format day/month/year $date = Get-Date -Format dd/MM/yy # Variables $MailFrom = '[email protected]' $MailTo = '[email protected]' $MailSubject = "Hyper-V Replica Report $date" $MailServer = 'smtp.yourdomain.coml' # Get replication status in HTML format $status = Get-VMReplication | Select-Object Name, State, Health, Mode, FrequencySec, PrimaryServer, ReplicaServer, ReplicaPort | ConvertTo-Html # Send email message Send-MailMessage -From $MailFrom -To $MailTo -Subject $MailSubject -BodyAsHtml -Body "$status" -SmtpServer $MailServer
Example output:
Name |
State |
Health |
Mode |
FrequencySec |
PrimaryServer |
ReplicaServer |
ReplicaPort |
VM01 |
Replicating |
Normal |
Replica |
300 |
HV-HOST01.domain.local |
HV-DR01.domain.local |
80 |
VM02 |
Replicating |
Normal |
Replica |
300 |
HV-HOST01.domain.local |
HV-DR01.domain.local |
80 |
VM03 |
Replicating |
Critical |
Replica |
300 |
HV-HOST02.domain.local |
HV-DR01.domain.local |
80 |
VM04 |
Replicating |
Normal |
Replica |
300 |
HV-HOST02.domain.local |
HV-DR01.domain.local |
80 |
VM05 |
Replicating |
Normal |
Replica |
300 |
HV-HOST03.domain.local |
HV-DR01.domain.local |
80 |
Technical Architect at Symity
Pingback: NeWay Technologies – Weekly Newsletter #174 – November 20, 2015 | NeWay
PS C:\Users\Administrador\Desktop> C:\Users\Administrador\Desktop\status.ps1
Send-MailMessage : El buzón de correo no está disponible. La respuesta del servidor fue: Access denied – Invalid HELO name (See RFC2821 4.1.1.1)
En C:\Users\Administrador\Desktop\status.ps1: 18 Carácter: 1
+ Send-MailMessage -From $MailFrom -To $MailTo -Subject $MailSubject -BodyAsHtml – …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Hello,
It would appear that you need to grant the server that is running the script permission to use the SMTP relay.
Regards
Chris
The email is being sent, but the body of the email is blank
If you add
Write-Host $status
After the “$status =” line, can you see the output?
Thank you!
Hi Chris, love the script and works manually but when using with schedule task there’s nothing in the body of the email. I would love to get this going but hitting a brick wall. I used Write-Host $status but didn’t help. Hoping you can head me in the right direction please? I cannot find anything that helps me.
Hi Shane, sorry for the long delay, I’ve just seen this message. I assume you may have resolved this by now? But I would assume the account running the scheduled task doesn’t have the correct permissions for Hyper-V?
how can you send to more than one email address
Hi, I am trying this script but get the following error.
Send-MailMessage :Unable to connect to the remote server
Pingback: Script PowerShell de notification par courrier électronique des réplications Hyper-V. – Delixir Pro /Blog