forked from RussellSageCollege/VeeamSlackNotifications
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Send-VeeamTeamsNotification.ps1
161 lines (108 loc) · 4.99 KB
/
Send-VeeamTeamsNotification.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]
$OrigJobName,
[Parameter(Mandatory)]
[string]
$Id
)
# Import Helper Functions
Import-Module "$PSScriptRoot\Helpers"
# Get the config from our config file
$Config = Get-Content -Path "$PSScriptRoot\config\VeeamTeamsNotificationConfig.json" -Raw | ConvertFrom-Json
# Should we log?
if( $Config.DebugEnable ) {
Start-Logging -Path $Config.DebugPath
}
# Add Veeam commands
Add-PSSnapin VeeamPSSnapin
# Get the Veeam session
$Session = Get-VBRBackupSession |
Where-Object { $_.OrigJobName -eq $OrigJobName -and $_.Id -eq $Id }
# Wait for the session to finish up
while ( -not $Session.IsCompleted ) {
Write-LogMessage 'Info' 'Session not finished Sleeping...'
Start-Sleep -Milliseconds 200
$Session = Get-VBRBackupSession |
Where-Object { $_.OrigJobName -eq $OrigJobName -and $_.Id -eq $Id }
}
# calculate success/warning/error counts
$TaskSessions = $Session.GetTaskSessions()
$SuccessCount = ( $TaskSessions | Where-Object Status -eq 'Success' ).Count
$WarningCount = ( $TaskSessions | Where-Object Status -eq 'Warning' ).Count
$FailureCount = ( $TaskSessions | Where-Object Status -eq 'Failed' ).Count
# setup variables
$SuccessFormat = if ( $SuccessCount ) { '{0} {1}' } else { '{0}' }
$SuccessIcon = '![Success Image](https://raw.githubusercontent.com/realslacker/VeeamTeamsNotifications/master/asset/img/icon/success.png)'
$WarningFormat = if ( $WarningCount ) { '{0} {1}' } else { '{0}' }
$WarningIcon = '![Warning Image](https://raw.githubusercontent.com/realslacker/VeeamTeamsNotifications/master/asset/img/icon/warning.png)'
$FailureFormat = if ( $FailureCount ) { '{0} {1}' } else { '{0}' }
$FailureIcon = '![Failure Image](https://raw.githubusercontent.com/realslacker/VeeamTeamsNotifications/master/asset/img/icon/error.png)'
$Duration = Convert-TimeSpanToHuman $Session.Progress.Duration
$Rate = '{0}/s' -f (Convert-BytesToHuman $Session.Progress.AvgSpeed)
$Bottleneck = $Session.Info.Progress.BottleneckInfo.Bottleneck
// Detailed bottleneck data
//$BottleneckSource = $Session.Info.Progress.BottleneckInfo.Source
//$BottleneckProxy = $Session.Info.Progress.BottleneckInfo.Proxy
//$BottleneckNetwork = $Session.Info.Progress.BottleneckInfo.Network
//$BottleneckTarget = $Session.Info.Progress.BottleneckInfo.Target
$Processed = '{0} ({1}%)' -f (Convert-BytesToHuman $Session.Progress.ProcessedUsedSize), $Session.Info.CompletionPercentage
$Read = Convert-BytesToHuman $Session.Progress.ReadSize
$Transferred = '{0} ({1:N1}x)' -f (Convert-BytesToHuman $Session.Progress.TransferedSize), ($Session.Progress.ReadSize / $Session.Progress.TransferedSize)
# set some status variables
switch ( $Session.Result ) {
Success {
$Title = "Veeam backup job '{0}' completed successfully" -f $Session.OrigJobName
$Color = '54B948'
}
Warning {
$Title = "Veeam backup job '{0}' completed with warnings" -f $Session.OrigJobName
$Color = 'F5BD4C'
}
Failed {
$Title = "Veeam backup job '{0}' has failed!" -f $Session.OrigJobName
$Color = 'EF5D4A'
}
None {
$Title = "Veeam backup job '{0}' completed" -f $Session.OrigJobName
$Color = '54B948'
}
default {
$Title = "Veeam backup job '{0}' has other status" -f $Session.OrigJobName
$Color = '54B948'
}
}
# build MessageCard
$TeamsJSON = @{
'@type' = 'MessageCard'
'@context' = 'http=//schema.org/extensions'
'correlationId' = $Session.Id
'themeColor' = $Color
'title' = $Title
'summary' = $Title
'sections'= @(
@{
'facts'= @(
@{ 'name' = 'Duration'; 'value' = $Duration }
@{ 'name' = 'Processing rate'; 'value' = $Rate }
@{ 'name' = 'Bottleneck'; 'value' = $Bottleneck }
@{ 'name' = 'Data Processed'; 'value' = $Processed }
@{ 'name' = 'Data Read'; 'value' = $Read }
@{ 'name' = 'Data Transferred'; 'value' = $Transferred }
@{ 'name' = 'Success'; 'value' = $SuccessFormat -f $SuccessCount, $SuccessIcon }
@{ 'name' = 'Warning'; 'value' = $WarningFormat -f $WarningCount, $WarningIcon }
@{ 'name' = 'Error'; 'value' = $FailureFormat -f $FailureCount, $FailureIcon }
)
}
)
}
# Build the web request
$Params = @{
Uri = $Config.WebhookURI
Method = 'Post'
Headers = @{'accept'='application/json'}
Body = $TeamsJSON | ConvertTo-Json -Depth 5
}
# Send It
$Request = Invoke-RestMethod @Params