Get-VeeamBackupReport.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID a0c2c3f8-c28c-42dd-bfde-6f541057e0af
 
.AUTHOR saw-friendship@yandex.ru
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS saw-friendship Veeam Html Report BackUpJob
 
.LICENSEURI
 
.PROJECTURI https://sawfriendship.wordpress.com/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Veeam backup job report.
 You can run this script from windows task scheduler and receive html report by Email
  
#>
 


[CmdletBinding()]
param(
[switch]$AsHtml,
[switch]$SendMail,
[string]$MailFrom,
[string]$MailTo,
[string]$MailSubject = 'VeeamReport',
[string]$MailServer
)

if(!(Get-PSSnapin -Name VeeamPSSnapin -ErrorAction SilentlyContinue)){
if((Get-PSSnapin -Registered -Name VeeamPSSnapin -ErrorAction SilentlyContinue)){
Add-PSSnapin VeeamPSSnapin
} else {Write-Warning "VeeamPSSnapin not exist"; break}
}

$join = ' :: '

$Jobs = Get-VBRJob -PipelineVariable Job | % {

try{
$LastBackup = $job.GetLastBackup()
$LastBackupStart = [datetime]($LastBackup.LastPointCreationTime)
$LastBackupEnd = [datetime]($LastBackup.MetaUpdateTime)
$LastBackupStartString = $LastBackupStart.ToString('yyyy-MM-dd HH:mm:ss')
$LastBackupEndString = $LastBackupEnd.ToString('yyyy-MM-dd HH:mm:ss')
if($LastBackupEnd -ge $LastBackupStart){
$LastBackupDuration = $LastBackupEnd - $LastBackupStart
$LastBackupDurationString = $LastBackupDuration.ToString('dd\.hh\:mm\:ss')
}
}catch{}

[pscustomobject][ordered]@{
Name = $Job.Name
LastResult = $job.GetLastResult()
LastBackupStart = $LastBackupStartString
LastBackupEnd = $LastBackupEndString
LastBackupDuration = $LastBackupDurationString
Object = $Job.GetObjectsInJob().Location -join $join

}
Remove-Variable LastBackup,LastBackupStart,LastBackupStartString,LastBackupEnd,LastBackupEndString,LastBackupDuration,LastBackupDurationString -ErrorAction SilentlyContinue

} | Sort-Object -Property LastBackupStart -Descending

if(!$AsHtml){
$OUT = $Jobs
} else {
$HtmlHead = @"
 <style type="text/css">
 table {border-collapse: collapse; width:100%;}
 th {border: 1px solid black; background-color:black; color:white;}
 td {border: 1px solid black;}
 </style>
"@

$PreContent = @"
 <table bgcolor=54b948 color=fffafa><tr><td>
 <h1>Veeam Report</h1>
 <h2>
 Job Success: $(($Jobs | ? {$_.LastResult -eq 'Success'}).count)<br>
 Job Warning: $(($Jobs | ? {$_.LastResult -eq 'Warning'}).count)<br>
 Job Failed: $(($Jobs | ? {$_.LastResult -eq 'Failed'}).count)<br>
 Job None: $(($Jobs | ? {$_.LastResult -eq 'None'}).count)<br>
 </h2></tr></td></table><br>
"@


$OUT = $Jobs | ConvertTo-Html -Head $HtmlHead -PreContent $PreContent
$OUT = $OUT -replace @($join,'<br>')
$OUT = $OUT -replace @('<table>','<table>')
$OUT = $OUT -replace @('<td>Success</td>','<td bgcolor=54b948>Success</td>')
$OUT = $OUT -replace @('<td>Warning</td>','<td bgcolor=ffd700>Warning</td>')
$OUT = $OUT -replace @('<td>Failed</td>','<td bgcolor=e66761>Failed</td>')
$OUT = $OUT -replace @('<td>None</td>','<td bgcolor=fffafa>None</td>')
}

if($SendMail){
Send-MailMessage -Encoding utf8 -Body ($OUT | Out-String) -BodyAsHtml -To $MailTo -From $MailFrom -Subject $MailSubject -SmtpServer $MailServer
} else {
$OUT
}