Skip to content

Instantly share code, notes, and snippets.

@andreagx
Last active January 3, 2016 19:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreagx/8507883 to your computer and use it in GitHub Desktop.
Save andreagx/8507883 to your computer and use it in GitHub Desktop.
The script send by email a report with Windows Server Backup activity and the target status.If backup fail or target space is under 10% the row in the table becomes with a red background.Location: C:\Users\Public\BackupLog.htm.Schedule script i.e. 2 hours before backup start time.
<#
andreagx.blogspot.com
The script send by email a report with Windows Server Backup activity and the target status
If backup fail or target space is under 10% the row in the table becomes with a red background
Location: C:\Users\Public\BackupLog.htm
Schedule script i.e. 2 hours before backup start time
http://andreagx.blogspot.it/2014/01/windows-server-backup-inviare-un-report.html
#>
#Specify here the name of your customer
$Customer = "MY CUSTOMER"
#hostname
$Hostname = $env:COMPUTERNAME
# Output file path
$path = $env:public
$OutputFile = $path + "\BackupLog.htm"
#Change culture to Us-us. Only for Windows Server 2012
$orgCulture = Get-Culture
[System.Threading.Thread]::CurrentThread.CurrentCulture = New-Object "System.Globalization.CultureInfo" "en-US"
# (Get-Date).AddDayS(-1) is from yesterday
$date = (Get-Date).AddDayS(-1)
#ID catched = 1,4,5,8,9,13,14,17,18,19,20,21,22,49,50,51,52,100,517,518,521,527,528,544,545,546,561,564,612
$evid_html = Get-WinEvent -ProviderName "Microsoft-Windows-Backup" |Where-Object {`
$_.ID -eq "1" -or`
$_.ID -eq "4" -or`
$_.ID -eq "5" -or`
$_.ID -eq "8" -or`
$_.ID -eq "9" -or`
$_.ID -eq "13" -or`
$_.ID -eq "14" -or`
$_.ID -eq "17" -or`
$_.ID -eq "18" -or`
$_.ID -eq "49" -or`
$_.ID -eq "20" -or`
$_.ID -eq "21" -or`
$_.ID -eq "22" -or`
$_.ID -eq "49" -or`
$_.ID -eq "50" -or`
$_.ID -eq "51" -or`
$_.ID -eq "52" -or`
$_.ID -eq "100" -or`
$_.ID -eq "517" -or`
$_.ID -eq "518" -or`
$_.ID -eq "519" -or`
$_.ID -eq "521" -or`
$_.ID -eq "527" -or`
$_.ID -eq "528" -or`
$_.ID -eq "544" -or`
$_.ID -eq "545" -or`
$_.ID -eq "546" -or`
$_.ID -eq "561" -or`
$_.ID -eq "564" -or`
$_.ID -eq "612" -and`
$_.TimeCreated -ge $date }`
|Select-Object TimeCreated , Message, ID, LevelDisplayName | Sort-Object TimeCreated -descending | ConvertTo-Html -PreContent "<p>Backup (Operational):</p>" -Fragment
[System.Threading.Thread]::CurrentThread.CurrentCulture = $orgCulture
#Get-Summary
$Summary = Get-WBSummary |Select-Object NumberOfVersions, CurrentOperationStatus, LastSuccessfulBackupTime, NextBackupTime | ConvertTo-Html -PreContent "<p>Backup (Summary):</p>" -Fragment
#Free Space on backup target.
$drives = Get-WBDisk | Where-Object {$_.ContainsBackup -eq "True"}
$drives_html = $drives | Select @{Name="Drive";Expression={$_.DiskName}},
@{Name="Size_GB";Expression={$_.TotalSpace/1GB -as [int]}},
@{Name="Used_GB";Expression={"{0:N2}" -f (($_.TotalSpace - $_.Freespace)/1GB) }},
@{Name="Free_GB";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }},
@{Name="Free_%";Expression={"{0:N2}" -f (($_.FreeSpace/$_.TotalSpace)*100) }} | ConvertTo-Html -PreContent "<p>Backup (Target):</p>" -Fragment
#Html
$header = "<p>Reporting Backup Status On: " + $hostname + " - " + " Customer: " + $Customer + "</p>"
$a = @"
<meta name="Andrea Gallazzi - andreagx.blogspot.com" content="Windows Server Backup Report"/>
<title>WSB Backup Report</title>
<style>
Body {
background-color: white;
font-family: Verdana;
font-size:12px;
color: white
}
Table{
border-width: 1px;
border-style: solid;
border-color: white;
border-collapse: collapse;
background-color: #4e6387;
width: 750px;
font-family: Verdana;
font-size: 12px;
color: white
}
Th{
border-width: 1px;
padding: 5px;
border-style: solid;
border-color: #F4F7FF;
background-color: #566B95;
color: #F4F7FF;
text-align: left;
font-family: Verdana;
font-size: 14px;
color: White;
}
Td{
border-width: 1px;
padding: 5px;
border-style: solid;
border-color: #F4F7FF;
font-family: Verdana;
font-size: 12px;
color: White;
}
H2{
color: black;
font-family: Verdana;
font-size: 14px;
color: #566B95
}
p{
font-family: Verdana;
font-size: 16px;
color: #566B95
}
</style>
"@
Add-Type -AssemblyName System.Xml.Linq
#Convert
$xml = [System.Xml.Linq.XDocument]::Parse( "$(ConvertTo-HTML -Title "Server Status" -head $a -Body "$evid_html $drives_html $Summary" )" )
# Find the index of the column you want to format:
$wsIndex1 = (($xml.Descendants("{http://www.w3.org/1999/xhtml}th") | Where-Object { $_.Value -eq "LevelDisplayName" }).NodesBeforeSelf() | Measure-Object).Count
$wsIndex2 = (($xml.Descendants("{http://www.w3.org/1999/xhtml}th") | Where-Object { $_.Value -eq "Free_%" }).NodesBeforeSelf() | Measure-Object).Count
# Format the column based on whatever rules you have:
switch($xml.Descendants("{http://www.w3.org/1999/xhtml}td") | Where { ($_.NodesBeforeSelf() | Measure).Count -eq $wsIndex1} )
{
{$_.Value -eq "Error" } { $_.Parent.SetAttributeValue( "style", "background: red;"); continue }
}
switch($xml.Descendants("{http://www.w3.org/1999/xhtml}td") | Where { ($_.NodesBeforeSelf() | Measure).Count -eq $wsIndex2} )
{
{$_.Value -le 10 } { $_.Parent.SetAttributeValue( "style", "background: red;"); continue }
}
# Save the html output into a file
$xml.Save("$OutputFile")
#Send Email
$bodycont = (Get-Content $OutputFile)
$emailSmtpServer = "smtp.domain.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "emailuserauth@domain.com"
$emailSmtpPass = "password"
$emailFrom = "Andrea Gallazzi <emailfrom@domain.com>"
$emailTo = "emailto1@domain.com,emailto2@domain.com"
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "## Backup report from: " + $Hostname + " - " + $Customer + " ##"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $bodycont
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment