Files
wsb-mailreport/wsb-mailreport.ps1

106 lines
6.6 KiB
PowerShell
Raw Permalink Normal View History

<#
.SYNOPSIS
Windows Server Backup - Mailreport
.DESCRIPTION
Dieses Script wertet den Status der Windows Server Sicherung aus
und versendet auf Basis der Daten eine Mail im HTML-Format über den
Status der letzten Sicherung. Zusätzlich werden Informationen über das
Sicherungsmedium und den freien Speicher übermittelt.
.EXAMPLE
./BackupMailReport.ps1
.NOTES
AUTHOR : Ralf Kirchner
EMAIL : ralf.kirchner@ksite.de
DATE : 09.08.2015
.LINK
http://www.ksite.de
#>
# Set sender address
$MailFrom = ""
# List of users to email the report (separate by comma)
# EXAMPLE: $MailTo = "user1@email", "user2@email"
$MailTo = ""
#SMTP server DNS name or IP address
$MailServer = ""
# Benutze Authentifikation (0 = nein, 1 = ja)
$MailAuth = 0
# SMTP Auth username
$MailAuthUser = ""
# SMTP Auth password
$MailAuthPass = ""
# DO NOT CHANGE ANYTHING PAST THIS LINE!
$OSVersionMajor = [System.Environment]::OSVersion.Version.Major
$OSVersionMinor = [System.Environment]::OSVersion.Version.Minor
# Required to use PowerShell with Windows Server 2008 Backup
if ($OSVersionMajor -eq 6 -And $OSVersionMinor -le 1) {
add-pssnapin windows.serverbackup
}
# Variables
$CurrentTime = Get-Date -Format F
$HostName = Get-Content env:computername
$BackupSummary = Get-WBSummary
$BackupLastSuccess = $BackupSummary.LastSuccessfulBackupTime
$BackupLastBackupTarget = $BackupSummary.LastBackupTarget
$BackupNextBackupTime = $BackupSummary.NextBackupTime
$BackupLastBackupTime = $BackupSummary.LastBackupTime
$BackupResult = $BackupSummary.LastBackupResultHR
$BackupErrorMsg = $BackupSummary.DetailedMessage
$BackupNumberOfVersions = $BackupSummary.NumberOfVersions
# Change Result of 0 to Success in green text and any other result as Failure in red text
if ($BackupResult -eq 0) {
$BackupResult = "Datensicherung erfolgreich abgeschlossen"
$BackupResultTitle = "Success"
$BackupResultColor = "green"
$MailBackupPriority = "Normal"
}
else {
$BackupResult = "Datensicherung mit Fehler abgeschlossen"
$BackupResultTitle = "Error"
$BackupResultColor = "red"
$MailBackupPriority = "High"
}
$DriveInfo = Get-WmiObject -Class win32_volume -Filter "Label='$BackupLastBackupTarget'"
$DriveFreeSpace = [math]::Round($DriveInfo.FreeSpace /1Gb, 2)
$DriveCapacity = [math]::Round($DriveInfo.Capacity /1Gb, 2)
$DriveUsedSpace = $DriveCapacity - $DriveFreeSpace
# Assemble the HTML Report
$HTMLMessage = @"
<html><head></head><body alink="#003366" leftmargin="0" link="#003366" marginheight="0" marginwidth="0" text="#000000" topmargin="0" vlink="#003366" ><table width="600" cellpadding="0" cellspacing="0" valign="top" align="center" border="0"><tr><td><font style="font-family:arial; font-size:24px; color:#c76e28; font-weight:bold; line-height:40px;">Windows Backup Report</font></td></tr><tr><td><table style="background-color: #eff5fb; border: 1px solid #6782a8;" width="100%"><tr><td><font style="font-family:arial; font-size:14px; color:#c76e28; font-weight:bold;">Backup Status</font></td></tr><tr><td><font style="font-family:arial; font-size:12px; color:$BackupResultColor; font-weight:bold;">$BackupResult !</font></td></tr></table></td></tr><tr><td><br /><table style="background-color: #eff5fb; border: 1px solid #6782a8;" width="100%"><tr><td colspan="2"><font style="font-family:arial; font-size:14px; color:#c76e28; font-weight:bold;">Allgemeine Infos</font></td></tr><tr><td width="220"><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">Letzte Sicherung:</font></td><td><font style="font-family:arial; font-size:13px;">$BackupLastBackupTime</font></td></tr><tr><td><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">Letzte erfolgreiche Sicherung:</font></td><td><font style="font-family:arial; font-size:13px;">$BackupLastSuccess</font></td></tr><tr><td><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">N&auml;chste Sicherung:</font></td><td><font style="font-family:arial; font-size:13px;">$BackupNextBackupTime</font></td></tr></table></td></tr><tr><td><br /><table style="background-color: #eff5fb; border: 1px solid #6782a8;" width="100%"><tr><td colspan="2"><font style="font-family:arial; font-size:14px; color:#c76e28; font-weight:bold;">Zielverwendung</font></td></tr><tr><td width="220"><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">Name:</font></td><td><font style="font-family:arial; font-size:13px;">$BackupLastBackupTarget</font></td></tr><tr><td><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">Kapazit&auml;t:</font></td><td><font style="font-family:arial; font-size:13px;">$DriveCapacity GB</font></td></tr><tr><td><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">Belegter Speicher:</font></td><td><font style="font-family:arial; font-size:13px;">$DriveUsedSpace GB</font></td></tr><tr><td><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">Freier Speicher:</font></td><td><font style="font-family:arial; font-size:13px;">$DriveFreeSpace GB</font></td></tr><tr><td><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">Verf&uuml;gbare Sicherungen:</font></td><td><font style="font-family:arial; font-size:13px;">$BackupNumberOfVersions Kopien</font></td></tr></table></td></tr><tr><td><br /><table style="background-color: #eff5fb; border: 1px solid #6782a8;" width="100%"><tr><td><font style="font-family:arial; font-size:14px; color:#c76e28; font-weight:bold;">Fehlermeldung (wenn verf&uuml;gbar)</font></td></tr><tr><td><font style="font-family:arial; font-size:13px; color:#004979; font-weight:bold;">$BackupErrorMsg</font></td></tr></table></td></tr><tr><td style="text-align: right;"><font style="font-family: Arial; color: #004979; font-weight: bold; font-size: 0.6em;">MailReport by <a href="http://www.ksite.de">www.ksite.de</a></font></td></tr></table></body></html>
"@
if ($MailAuth) {
# set SMTP cerdentials
$SecuredPassword = ConvertTo-SecureString -String "$MailAuthPass" -AsPlainText -Force
$SecuredCredentials = New-Object System.Management.Automation.PSCredential($MailAuthUser,$SecuredPassword)
# Email the report
Send-MailMessage -from $MailFrom -to $MailTo -subject "$HostName Windows Backup $BackupResultTitle" -Encoding UTF8 -BodyAsHTML -body $HTMLMessage -priority $MailBackupPriority -Credential $SecuredCredentials -smtpServer $MailServer
} else {
# Email the report
Send-MailMessage -from $MailFrom -to $MailTo -subject "$HostName Windows Backup $BackupResultTitle" -Encoding UTF8 -BodyAsHTML -body $HTMLMessage -priority $MailBackupPriority -smtpServer $MailServer
}