Windows Server Backup 电子邮件通知 Powershell 脚本

Windows Server Backup 电子邮件通知 Powershell 脚本

我希望下面的一切都有意义,任何帮助都将不胜感激。

我正在尝试创建一个脚本,该脚本会通知我 Windows Server Backup 是否成功或在服务器列表上失败。

使用任务计划程序将脚本设置为在特定时间运行。如果我使用管理员帐户,脚本可以正常运行。但是,我尝试使用权限有限的用户帐户运行脚本,但失败了。

如果用户帐户属于“事件日志读取器”和“远程管理用户”,则脚本将无法正常运行,并且事件查看器日志中会出现错误 - 事件 ID 1016,来源 DistributedCOM

应用程序特定的权限设置未向用户“我的用户帐户”授予 CLSID 为 {37734C4D-FFA8-4139-9AAC-60FBE55BF3DF} 和 APPID 为 {C3B65D83-FB15-4E3F-BA04-097D1E2B5AC1} 的 COM 服务器应用程序的本地激活权限

但是,如果我将用户帐户添加到“备份操作员”组和“远程管理用户”组,它就可以正常运行。

我有点困惑为什么会发生这种情况。我以为“事件日志读取器”组会让您读取事件 ID。

以下是我的 Poweshell 脚本。我从以下网站获取了该脚本:http://blog.jocha.se/tech/wbadmin-backup-mail-report- 我做了一些改变。

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 = "[email protected]" # List of users to email your report to (separate by comma)
$MailMessageFrom = "[email protected]" # 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
}
}

我可能也完全误解了一切:)

答案1

Get-WBJob 通过服务器备份模块访问日志。该模块允许您管理备份作业,因此您需要成为备份操作员才能使用许多(/大多数?/所有?)命令。

相关内容