<# .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 = @"
Windows Backup Report
Backup Status
$BackupResult !

Allgemeine Infos
Letzte Sicherung:$BackupLastBackupTime
Letzte erfolgreiche Sicherung:$BackupLastSuccess
Nächste Sicherung:$BackupNextBackupTime

Zielverwendung
Name:$BackupLastBackupTarget
Kapazität:$DriveCapacity GB
Belegter Speicher:$DriveUsedSpace GB
Freier Speicher:$DriveFreeSpace GB
Verfügbare Sicherungen:$BackupNumberOfVersions Kopien

Fehlermeldung (wenn verfügbar)
$BackupErrorMsg
MailReport by www.ksite.de
"@ 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 }