Windows PowerShell script to email log file

I’m new to Restic and it is, by far, the best backup utility I’ve ever used. I did a bit of searching to find a script for Windows to email a backup report with little success. However, I cobbled this PowerShell script together from some various scripts I found on other sites. It will email the contents of your log file (or any other text file). I’ve incorporated it into a larger Restic script which is run through Windows Task Scheduler. Hope it helps those looking for similar.

$Username = "username";
$Password = "password";

function Send-ToEmail([string]$email){

    $message = new-object Net.Mail.MailMessage;
    $message.From = "sender@email.com";
    $message.To.Add($email);
    $smtp = new-object Net.Mail.SmtpClient("smtp.email.com", "587");
    $smtp.EnableSSL = $true;
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);

    $message.Subject = "Restic Log";
    $LogTime = [String](Get-ItemProperty -Path c:\restic\restic_log.txt -Name LastWriteTime).LastWriteTime;
    $LogFile = Get-Content c:\restic\restic_log.txt -Raw;
    $message.Body = "Time stamp of log file: " + $LogTime + "`n`n" + $LogFile;
    $smtp.send($message);
 }
Send-ToEmail  -email "recipient@email.com";
2 Likes

Thanks for sharing!

1 Like