Windows Server Backup Checker PowerShell Script

This PowerShell script was written with the intention of automating the manual checking of Windows Server Backup on 10 or so physical Windows servers running Server 2012 and 2008 scattered across the country. Generally these backups run without issue so I decided it would be best to be alerted only to failures while keeping a log of every outcome.

The script will take a list of server names in two plain text files; one for servers running Windows Server 2012 and the other for those running 2008, aptly named 2012servers.txt and 2008servers.txt. It will then run the Get-WBSummary cmdlet against each of the servers in the text files and check the contents of LastBackupResultHR. 0 is a successful backup and anything else is a failure of some sort. If a failure is spotted the script will then email these results along with the error description (which may or may not be helpful) and the server’s name. A .csv is created and each outcome is appended to this log.

# Backup checker script
# Ross Sutherland 2016

$2012servers = Get-Content 2012servers.txt
$2008servers = Get-Content 2008servers.txt

$emailSMTP = "smtp.your.domain.com" 
$emailTo = "Your Name <your.name@your.domain.com>" 
$emailFrom = "Backup Admin <backups@your.domain.com>" 
$emailSubject = "Backup report"

foreach($2012server in $2012servers){    
        $backupResults = Invoke-Command -ScriptBlock { Get-WBSummary } -ComputerName $2012server -ErrorAction SilentlyContinue
        $backupResults | Export-Csv -Path .\$($2012server)_backuplog.csv -Append
    if($backupResults -eq $null){
        Write-Host "Something went wrong while retreiving backup summary from $2012server :(" -BackgroundColor Yellow -ForegroundColor Red
    }else{
        if($backupResults.LastBackupResultHR -ne 0){
            Write-Host "Backup on $($backupResults.PSComputerName) failed :(" -ForegroundColor Red
            Send-MailMessage -SmtpServer $emailSMTP -To $emailTo -From $emailFrom -Subject $emailSubject -Body `
            "Backup on $($backupResults.PSComputerName) failed with error: $($backupResults.DetailedMessage)" -BodyAsHtml -Priority high
        }else{
            Write-Host "Backup on $($backupResults.PSComputerName) completed successfully on $($backupResults.LastSuccessfulBackupTime) :)" -ForegroundColor Green
        }
    }
}

foreach($2008server in $2008servers){    
        $backupResults = Invoke-Command -ScriptBlock { add-pssnapin windows.serverbackup; Get-WBSummary } -ComputerName $2008server -ErrorAction SilentlyContinue
        $backupResults | Export-Csv -Path .\$($2008server)_backuplog.csv -Append
    if($backupResults -eq $null){
        Write-Host "Something went wrong while retreiving backup summary from $2008server :(" -BackgroundColor Yellow -ForegroundColor Red
    }else{
        if($backupResults.LastBackupResultHR -ne 0){
            Write-Host "Backup on $($backupResults.PSComputerName) failed :(" -ForegroundColor Red
            Send-MailMessage -SmtpServer $emailSMTP -To $emailTo -From $emailFrom -Subject $emailSubject -Body `
            "Backup on $($backupResults.PSComputerName) failed with error: $($backupResults.DetailedMessage)" -BodyAsHtml -Priority high
        }else{
            Write-Host "Backup on $($backupResults.PSComputerName) completed successfully on $($backupResults.LastSuccessfulBackupTime) :)" -ForegroundColor Green
        }
    }
}

As you can see the script essentially runs twice for each version of Windows Server. This is simply because the windows.serverbackup snappin must be loaded on Server 2008 for the Get-WBSummary cmdlet to run but loading this snappin on 2012 throws an error. There might be a nice way of having PowerShell detect the version of Windows the server is running before issuing the command but that’s a little beyond me at this stage.

backup_email

Backup failure sample email

If you’d like to run this in your own environment you must have the following:

  • An SMTP server to relay through (without authentication)
  • Permissions to administer backups

Ideas for improvement:

  • Ability to email through secure SMTP servers
  • Better error handling if Get-WBSummary fails
  • Rotate the log file after a certain amount of time or when it reaches a certain size
  • Detect the Windows version and run the appropriate command

Ross

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code