Skip to content

Instantly share code, notes, and snippets.

@TimMillerDyck
Created May 29, 2022 21:55
Show Gist options
  • Save TimMillerDyck/a5891798938f912d7d0b4ab5cdf4ab43 to your computer and use it in GitHub Desktop.
Save TimMillerDyck/a5891798938f912d7d0b4ab5cdf4ab43 to your computer and use it in GitHub Desktop.
E-mail recent Windows Backup Event Log events to monitor backup status
<#
E-mail recent Windows Backup Event Log events to monitor backup status
This script is intended to run via an OS scheduled task using Windows Task Scheduler.
The script runs under the local computer account e.g. "myuser".
This account needs to have local admin rights to access the Windows Event Log.
To run the script manually:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -ExecutionPolicy bypass -File "C:\Scripts\WindowsBackupEMailEvents.ps1"
To set up the sending mail credential, perform the following one-time setup **while logged in as the user that will be running the script**.
The encryption uses a user-specific secret managed by Windows.
runas /user:myuser powershell.exe
$SendAccountUserPassword = ConvertTo-SecureString 'SecretPasswordForSendingAccount' -AsPlainText -Force
$SendAccountUserPassword | ConvertFrom-SecureString | Set-Content "C:\Scripts\sendaccountuser_encrypted_password.txt"
Changelog
2022-04-21 Tim Miller Dyck initial release
2022-05-15 Tim Miller Dyck increase width of event message text from 100 characters to 200 characters
2022-05-29 Tim Miller Dyck fetch only the most recent Windows Backup success message to handle the case where there was more than one successfull backup in the measurement period
#>
# initialize variables
$EventLogName = "Application"
$EventSourceName = "Windows Backup"
$StartTime = (Get-Date).AddDays(-7) # check event data for the previous 7 days
$EventSuccessId = 4098 # this is the "Backup completed successfully" message
$StatusEventMessage = "WARNING: No recent successful backup event found"
$FromEMail = noreply@mydomain.org
$ToEMail = myemail@mydomain.org
# load the sending e-mail address credential
$EncryptedPassword = Get-Content "C:\Scripts\sendaccountuser_encrypted_password.txt" | ConvertTo-SecureString
$FromCredential = New-Object System.Management.Automation.PsCredential($FromEMail, $EncryptedPassword)
# get all recent Windows Backup events
$AllBackupEvents = Get-WinEvent -ErrorAction SilentlyContinue -FilterHashtable @{
Logname=$EventLogName
ProviderName=$EventSourceName
StartTime=$StartTime
}
# get the most recent single Windows Backup success event within the search time period (by default, events are returned in newest-first order)
$SuccessBackupEvents = Get-WinEvent -MaxEvents 1 -ErrorAction SilentlyContinue -FilterHashtable @{
Logname=$EventLogName
ProviderName=$EventSourceName
StartTime=$StartTime
Id=$EventSuccessId
}
# check if a success event was found
if ($SuccessBackupEvents -ne $null -and $SuccessBackupEvents.Message.Contains("success")) {
$StatusEventMessage = $SuccessBackupEvents.Message
}
# assemble the e-mail message contents and sending configuration
$MailParams = @{
SmtpServer = 'smtp.office365.com'
Port = '587'
UseSSL = $true
Credential = $FromCredential
From = $FromEMail
To = $ToEMail
Subject = "Windows Backup status: $StatusEventMessage"
Body = "<h2>Windows Backup status report</h2>
<p><b>$StatusEventMessage</b></p>
<p>Here are all Windows Backup status events since $($StartTime.ToLongDateString()).</p>
<p><pre>$($AllBackupEvents | Format-Table -Autosize | Out-String -Width 200)</pre></p>"
}
# send the e-mail
Send-MailMessage -BodyAsHtml @MailParams
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment