Last Updated:

MailReport – Windows Server Sicherung

Ralf Kirchner
Ralf Kirchner Windows

Backup abgeschlossen? Eine automatische Mail schafft Klarheit.

Früher zeigte ein ausgeworfenes Band das Ende der Sicherung – heute nutzen viele Kunden USB-Wechsellaufwerke, was leicht zu Problemen führt, wenn das Medium zu früh gewechselt wird. Um das zu vermeiden, richteten wir für einen Kunden eine ereignisbasierte Aufgabe auf dem Windows-Server ein. Diese startet nach Abschluss der Sicherung ein PowerShell-Skript, das einen HTML-Report erstellt und per E-Mail versendet – so weiß der Kunde genau, wann der Tausch sicher ist.

Erstellen Sie zuerst eine Powershell-Skript mit dem Editor und speichern Sie es auf dem Server als „BackupMailReport.ps1“. Passen Sie danach die Variablen Ihren Gegebenheiten an:

BackupMailReport.ps1

<#

.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   : 
  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
}

Als nächstes wird eine Ereignisbasierende Aufgabe benötigt, welche das gerade erstellte Powershell-Skript ausführt. Für den Task „BackupMailReport‘ werden zwei Trigger konfiguriert, die bei den folgenden Events starten:
1. Ereignis-ID 14 (Abschluss der Sicherung, egal ob erfolgreich oder nicht erfolgreich)
2. Ereignis-ID 49 (Kein Sicherungsziel gefunden)

Image description
Image description

Als Aktion für diese Aufgabe muss „Programm starten“ ausgewählt und „powershell.exe“ eingetragen werden. Unter „Argument hinzufügen“ wird dann als Parameter „-command c:\BackupMailReport.ps1“ eingetragen. Der Pfad zu dem Script muss den Gegebenheiten angepasst werden.

Image description

Nun wird nach jeden durchgeführten Backup eine Mail mit dem Status der Sicherung an die hinterlegte Mailaddresse(n) versandt. Dieses Script wurde unter Windows Server 2008/2008R2 und Windows Server 2012/2012R2 erfolgreich getestet.