Windows Server Backup Email Notification Powershell Script

backuppowershelltask-schedulerwindowswindows-server-2012-r2

I hope everything below makes sense, any help would be much appreciated.

I'm trying to create a script that will notify me if Windows Server Backup was successful or if it failed on a list of servers.

The script is set to run at a certain time using Task Scheduler. The script runs fine if I use an Administrator account. However, I'm trying to run the script with a user account with limited permissions and it's failing.

If the user account is part of the "Event Log Readers" and "Remote Management Users", the script will not run properly and there is an error in the Event Viewer logs – Event ID 1016, Source DistributedCOM

The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID
{37734C4D-FFA8-4139-9AAC-60FBE55BF3DF}
and APPID
{C3B65D83-FB15-4E3F-BA04-097D1E2B5AC1}
to the user "My user account"

However, if I add the user account to the "Backup Operators" group and the "Remote Management Users" group, it runs fine.

I'm a bit confused as to why this would happen. I was under the impression that "Event Log Reader" group would let you read Event IDs.

Below is my Poweshell script. I got the script from – http://blog.jocha.se/tech/wbadmin-backup-mail-report – I've made a few changes.

Add-PSSnapin Windows.ServerBackup -ErrorAction SilentlyContinue

#######################################
#-------- Variables to change --------#

$Servers = New-PSSession -Computername server1,server2
Invoke-Command -Session $Servers {

# Set your Company name
$Company = "xxxx"

# Set the recipient/sender email-address
$MailMessageTo = "recipient@company.com" # List of users to email your report to (separate by comma)
$MailMessageFrom = "sender@company.com" # Enter the email you would like the report sent from
$MailMessageSMTPServer = "X.X.X.X" # Enter your own SMTP server DNS name / IP address here
$MailMessagePriority = "Normal" # Low/Normal/High
$HTMLMessageSubject = $env:computername+": Backup Report - "+(Get-Date) # Email Subject


#---- Don't change anything below ----#
#######################################

Try {
$CurrentTime = (Get-Date).ToString("yyyy-MM-dd HH:mm")
$Computer = Get-Content env:computername
$WBJob = Get-WBJob -Previous 1
$WBSummary = Get-WBSummary
$WBLastSuccess = ($WBSummary.LastSuccessfulBackupTime).ToString("yyyy-MM-dd HH:mm")
$WBResult = $WBSummary.LastBackupResultHR
$WBErrorMsg = $WBJob.ErrorDescription + "`n" + $WBSummary.DetailedMessage
$WBStartTime = $WBJob.StartTime
$WBEndTime = $WBJob.EndTime
$WBDuration = (New-TimeSpan -Start $WBStartTime -End $WBEndTime)



#$Password = ConvertTo-SecureString $MailPassword -AsPlainText -Force
#$Credentials = New-Object System.Management.Automation.PSCredential ($MailUser, $Password)

Function FormatBytes
{
 Param
 (
 [System.Int64]$Bytes
 )
 [string]$BigBytes = ""
 #Convert to TB
 If ($Bytes -ge 1TB) {$BigBytes = [math]::round($Bytes / 1TB, 2); $BigBytes += " TB"}
 #Convert to GB
 ElseIf ($Bytes -ge 1GB) {$BigBytes = [math]::round($Bytes / 1GB, 2); $BigBytes += " GB"}
 #Convert to MB
 ElseIf ($Bytes -ge 1MB) {$BigBytes = [math]::round($Bytes / 1MB, 2); $BigBytes += " MB"}
 #Convert to KB
 ElseIf ($Bytes -ge 1KB) {$BigBytes = [math]::round($Bytes / 1KB, 2); $BigBytes += " KB"}
 #If smaller than 1KB, leave at bytes.
 Else {$BigBytes = $Bytes; $BigBytes += " Bytes"}
 Return $BigBytes
}

Function Log-BackupItems
{
    Param
    (
        [System.String]$Name,
        [System.String]$Status,
        [System.Int64]$Bytes
    )
    $Item = New-Object System.Object;
    $Item | Add-Member -Type NoteProperty -Name "Name" -Value $Name;
    $Item | Add-Member -Type NoteProperty -Name "Status" -Value $Status;
    $Item | Add-Member -Type NoteProperty -Name "Size" -Value (FormatBytes -Bytes $Bytes);
    Return $Item;
}

$results=@()
$WBJob | % {
 $_.JobItems | % {
 $BackupItem = $null
 If ($_.Name -eq 'VolumeList') {
 $_ | % {$_.SubItemList | % {
 $BackupItem = Log-BackupItems -Name $_.Name -Status $_.State -Bytes $_.TotalBytes
 $results += $BackupItem
 }}
 } 
 Else {
 $_ | % {
 $BackupItem = Log-BackupItems -Name $_.Name -Status $_.State -Bytes $_.TotalBytes
 $results += $BackupItem
 }
 }
 }
}

# Change Result of 0 to Success in green text and any other result as Failure in red text
If ($WBResult -eq 0) { $WBResult = "Successful"}
Else {$WBResult = "Failed"}

# Assemble the HTML Report
$HTMLMessage = @"
    <!DOCTYPE html>
    <html>
    <head>
    <title>$Company Backup Report for $Computer</title>
    <style>
    body { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 12px }
    h3{ clear: both; font-size: 150%; margin-left: 20px;margin-top: 30px; }
    table { padding: 15px 0 20px; width: 500px; text-align: left; }
    td, th { padding: 0 20px 0 0; margin 0; text-align: left; }
    th { margin-top: 15px }
    a, a:visited { color: #2ea3f2; text-decoration: none; }
    #Report { width: 600px; }
    #Successful { color: green }
    #Failed { color: red }
    </style>
    </head>
    <body>
    <div id="Report">
    <p><h3><a>$Company Backup Report for $Computer</a></p></h3>
    <table id="summary"><tbody>
    <tr><td>Todays date:</td>
    <td>$CurrentTime</td></tr>
    <tr><td>Last Successful Backup:</td>
    <td>$WBLastSuccess</td></tr>
    <tr><td>Start time last backup:</td>
    <td>$WBStartTime</td></tr>
    <tr><td>End time last backup:</td>
    <td>$WBEndTime</td></tr>
    <tr><td>Duration last backup:</td>
    <td>$WBDuration</td></tr>
    <tr><td>Backup Result:</td>
    <td><b id="$WBResult">$WBResult</b></td></tr>
    <tr><td>Error Message (if applicable):</td>
    <td>$WBErrorMsg</td></tr></tbody></table>

    $(
     $html = $results | ConvertTo-HTML -Fragment
     $xml=[xml]$html
     $attr=$xml.CreateAttribute('id')
     $attr.Value='items'
     $xml.table.Attributes.Append($attr) | out-null
     $html=$xml.OuterXml | out-string
     $html
    )
    </div>
    </body>
    </html>
"@

# Email the report
$MailMessageOptions = @{
    From            = "$MailMessageFrom"
    To              = "$MailMessageTo"
    Subject         = "$WBResult : $HTMLMessageSubject"
    BodyAsHTML      = $True
    Body            = "$HTMLMessage"
    Priority        = "$MailMessagePriority"
    SmtpServer      = "$MailMessageSMTPServer"
}
Send-MailMessage @MailMessageOptions
}

Catch {
$MailMessageOptions = @{
    From            = "$MailMessageFrom"
    To              = "$MailMessageTo"
    Subject = "Failed Backup on $Computer"
    BodyAsHTML      = $True
    Body = "The backup script failed to run!"
    Priority        = "$MailMessagePriority"
    SmtpServer      = "$MailMessageSMTPServer"
    }
    Send-MailMessage @MailMessageOptions
}
}

I might be completely misunderstanding everything also 🙂

Best Answer

Get-WBJob is accessing the logs through the Server Backup module. That module lets you manage Backup Jobs, ergo you need to be a Backup Operator to use many(/most?/all?) of the commands.

Related Topic