Created
March 28, 2022 22:29
-
-
Save benyoungnz/a0ae8952c2f5a447a7cb481c5d1ad39d to your computer and use it in GitHub Desktop.
Powershell Report script to alert when total backup job size (storage usage) is over a threshold - so you can monitor it and or split jobs into nice chunks if you so desire.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#alert at or over this amount | |
$alertOverGB = 100; | |
#your email settings | |
$emailSettings = @{ | |
To = 'you@example.local' | |
From = 'backupreports@example.local' | |
Smtpserver = 'smtp.example.local' | |
Subject = "{{VBRServer}} jobs over $($alertOverGB)GB" | |
} | |
#measure all the jobs and their sizes | |
$vbrAllJobSizes = Get-VBRBackup | Select @{N="Job";E={$_.Name}}, @{N="Usage";E={[math]::Round(($_.GetAllStorages().Stats.BackupSize | Measure-Object -Sum).Sum/1GB,1)}} | |
#filter and sort where over our threshold | |
$vbrJobsInAlert = $vbrAllJobSizes | where {$_.Usage -gt $alertOverGB} | Sort-Object -Property Usage -Descending | |
# if results over threshold, send email | |
if ($vbrJobsInAlert.count -ne 0) { | |
#base email theme | |
$emailTemplate = @" | |
<!doctype html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<style> | |
@media only screen and (max-width: 620px) { | |
table.body h1 { | |
font-size: 28px !important; | |
margin-bottom: 10px !important; | |
} | |
table.body p, | |
table.body ul, | |
table.body ol, | |
table.body td, | |
table.body span, | |
table.body a { | |
font-size: 16px !important; | |
} | |
table.body .wrapper, | |
table.body .article { | |
padding: 10px !important; | |
} | |
table.body .content { | |
padding: 0 !important; | |
} | |
table.body .container { | |
padding: 0 !important; | |
width: 100% !important; | |
} | |
table.body .main { | |
border-left-width: 0 !important; | |
border-radius: 0 !important; | |
border-right-width: 0 !important; | |
} | |
} | |
@media all { | |
.apple-link a { | |
color: inherit !important; | |
font-family: inherit !important; | |
font-size: inherit !important; | |
font-weight: inherit !important; | |
line-height: inherit !important; | |
text-decoration: none !important; | |
} | |
#MessageViewBody a { | |
color: inherit; | |
text-decoration: none; | |
font-size: inherit; | |
font-family: inherit; | |
font-weight: inherit; | |
line-height: inherit; | |
} | |
} | |
</style> | |
</head> | |
<body style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;"> | |
<span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">{{VBRServer}} jobs have been processed, some are over threshold of {{Threshold}}GB.</span> | |
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f6f6f6; width: 100%;" width="100%" bgcolor="#f6f6f6"> | |
<tr> | |
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top"> </td> | |
<td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; max-width: 580px; padding: 10px; width: 580px; margin: 0 auto;" width="580" valign="top"> | |
<div class="content" style="box-sizing: border-box; display: block; margin: 0 auto; max-width: 580px; padding: 10px;"> | |
<!-- container --> | |
<table role="presentation" class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background: #ffffff; border-radius: 3px; width: 100%;" width="100%"> | |
<!-- main--> | |
<tr> | |
<td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;" valign="top"> | |
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;" width="100%"> | |
<tr> | |
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top"> | |
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;"><h2>{{Title}}</h2></p> | |
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">Backup jobs have been processed, below are the jobs that meet your threshold of {{Threshold}}GB.</p> | |
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; box-sizing: border-box; width: 100%;" width="100%"> | |
<tbody> | |
<tr> | |
<td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;" valign="top"> | |
<table role="presentation" border="0" cellpadding="10" cellspacing="10" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;"> | |
<thead> | |
<tr><th cellpadding="10" style="text-align: left;" align="left">Job</th> | |
<th cellpadding="10" style="text-align: left;" align="left">Size</th></tr> | |
<thead> | |
<tbody> | |
{{DataRows}} | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">Over to you!</p> | |
</td> | |
</tr> | |
</table> | |
</td> | |
</tr> | |
<!-- end main --> | |
</table> | |
<!-- end container --> | |
<!-- footer --> | |
<div class="footer" style="clear: both; margin-top: 10px; text-align: center; width: 100%;"> | |
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;" width="100%"> | |
<tr> | |
<td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; color: #999999; font-size: 12px; text-align: center;" valign="top" align="center"> | |
<span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">Generated {{GeneratedOn}} for {{VBRServer}}</span> | |
</td> | |
</tr> | |
</table> | |
</div> | |
<!-- end footer --> | |
</div> | |
</td> | |
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top"> </td> | |
</tr> | |
</table> | |
</body> | |
</html> | |
"@ | |
#this is used as the template for the jobs over threshold in the data table | |
$rowTemplate = @" | |
<tr> | |
<td cellpadding="10" style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top" align="left" width="75%">{{JobName}}</td> | |
<td cellpadding="10" style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top" align="left" width="25%">{{JobUsage}}GB</td> | |
</tr> | |
"@ | |
#append to this and we will insert into email soon | |
$alertRows = "" | |
foreach ($jobAlerts in $vbrJobsInAlert) | |
{ | |
$row = $rowTemplate | |
$row = $row.Replace("{{JobName}}", $jobAlerts.Job) | |
$row = $row.Replace("{{JobUsage}}", $jobAlerts.Usage) | |
$alertRows += $row | |
} | |
#put server details in of current backup server | |
$vbrSession = Get-VBRServerSession | |
#show server detail in subject | |
$emailSettings.Subject = $emailSettings.Subject.Replace("{{VBRServer}}", $vbrSession.Server) | |
#replace vars from template with data | |
$emailTemplate = $emailTemplate.Replace("{{Title}}", $emailSettings.Subject) | |
$emailTemplate = $emailTemplate.Replace("{{Threshold}}", $alertOverGB) | |
$emailTemplate = $emailTemplate.Replace("{{GeneratedOn}}", $(Get-Date -Format "dd/MM/yyyy HH:mm")) | |
$emailTemplate = $emailTemplate.Replace("{{DataRows}}", $alertRows) | |
$emailTemplate = $emailTemplate.Replace("{{VBRServer}}", $vbrSession.Server) | |
# send email | |
Send-MailMessage @emailSettings -Body $emailTemplate -BodyAsHtml | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment