Tuesday, 25 September 2018

Send daily HTML email with outstanding Hyper-V checkpoints with encrypted password information



Here’s a script I’ve written that can be added as a scheduled task on the SCVMM server to send a daily report to selected people containing all the currently outstanding checkpoints on the Hyper-V hosts.  This is done without having to store any plain text passwords (they are stored but in an encrypted format).  It formats the email as HTML so that it looks a bit prettier.  I could have made the HTML fancier but I wanted to keep the script simple.



$Header = @"


       TABLE {font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif; border: 1px solid #FFFFFF; background-color: #FFFFFF; width: 750px; height: 100px; text-align: center; border-collapse: collapse;}

       TH {background: #FF0000; border-bottom: 5px solid #FFFFFF; }

       TD { border: 1px solid #FFFFFF;}

       tr:nth-child(odd) {background: #D0CFD1; padding: 3px 2px; }




$Transpath = "C:\temp\logs\snapshot_report.log"

$DateTime = (Get-Date).ToString('dd/MM/yy hh:mm:ss')

$password = cat C:\scripts\securestring.txt | ConvertTo-SecureString

$mycred = New-Object System.Management.Automation.PSCredential ("username",$password)

$Date = Get-Date -Format "dd-MM-yyyy"

$Subject = "Daily Snapshot Report from $env:computername for $Date"

$Greeting = "Good morning, please find below the daily snapshot report.  For any queries on this report please contact Tom K `(info`@cloudwyse.co.uk`)"

$ReportText = Get-SCVirtualMachine | Get-VMCheckpoint | Select VM,Name,Description,AddedTime | ConvertTo-HTML -Head $Header -Body "$Greeting $_ " | Out-String

$Recipients = 'email1@cloudwyse.co.uk','email2@cloudwyse.co.uk','email3@cloudwyse.co.uk'


Start-Transcript -Path "$transpath" -Append

Write-Host "Started running script on $env:computername at $DateTime"


Send-MailMessage `

       -SmtpServer smtpserver.contoso.com `

       -Credential $mycred `

       -From 'reports@cloudwyse.co.uk' `

       -To $Recipients `

       -Subject $Subject `

       -Body $ReportText -BodyAsHtml `

       -Port 587 -UseSsl


Write-Host "Finished running script on $env:computername at $DateTime"




Follow the instructions here to create the securestring.txt



No comments:

Post a comment