forked from RussellSageCollege/VeeamSlackNotifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlackVeeamAlertSender.ps1
65 lines (53 loc) · 1.92 KB
/
SlackVeeamAlertSender.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
Param(
[String]$JobName,
[String]$Id
)
####################
# Import Functions #
####################
Import-Module "$PSScriptRoot\Helpers"
# Get the config from our config file
$config = (Get-Content "$PSScriptRoot\config\vsn.json") -Join "`n" | ConvertFrom-Json
# Should we log?
if($config.debug_log) {
Start-Logging "$PSScriptRoot\log\debug.log"
}
# Add Veeam commands
Add-PSSnapin VeeamPSSnapin
# Get the session
$session = Get-VBRBackupSession | ?{($_.OrigJobName -eq $JobName) -and ($Id -eq $_.Id.ToString())}
# Wait for the session to finish up
while ($session.IsCompleted -eq $false) {
Write-LogMessage 'Info' 'Session not finished Sleeping...'
Start-Sleep -m 200
$session = Get-VBRBackupSession | ?{($_.OrigJobName -eq $JobName) -and ($Id -eq $_.Id.ToString())}
}
# Save same session info
$Status = $session.Result
$JobName = $session.Name.ToString().Trim()
$JobType = $session.JobTypeString.Trim()
# Switch on the session status
switch ($Status) {
None {$emoji = ':thought _ balloon: '}
Warning {$emoji = ':angry: '}
Success {$emoji = ':green_heart: '}
Failed {$emoji = ':fire: '}
Default {$emoji = ''}
}
# Build the details string
$details = "Backup Size - " + [String]$session.BackupStats.BackupSize + " / Data Size - " + [String]$session.BackupStats.DataSize + " / Dedup Ratio - " + [String]$session.BackupStats.DedupRatio + " / Compress Ratio - " + [String]$session.BackupStats.CompressRatio
# Build the payload
$slackJSON = @{}
$slackJSON.channel = $config.channel
$slackJSON.username = $config.service_name
$slackJSON.icon_url = $config.icon_url
$slackJSON.text = $emoji + '*Job:* ' + $JobName + "`n" + $emoji + '*Status:* ' + $Status + "`n" + $emoji + '*Details:* ' + $details
# Build the web request
$webReq=@{
Uri = $config.webhook
ContentType = 'application/json'
Method = 'Post'
body = ConvertTo-Json $slackJSON
}
# Send it to Slack
$request = Invoke-WebRequest -UseBasicParsing @webReq