11# Define parameters
2- Param (
2+ param (
33 [String ]$jobName ,
44 [String ]$id ,
55 [String ]$jobType ,
99
1010# Function to get a session's bottleneck from the session logs
1111# See https://github.com/tigattack/VeeamNotify/issues/19 for more details.
12- Function Get-Bottleneck {
12+ function Get-Bottleneck {
1313 param (
1414 $Logger
1515 )
@@ -20,10 +20,10 @@ Function Get-Bottleneck {
2020 Select-Object - ExpandProperty Title) `
2121 -replace ' Primary bottleneck:' , ' '
2222
23- If ($bottleneck.Length -eq 0 ) {
23+ if ($bottleneck.Length -eq 0 ) {
2424 $bottleneck = ' Undetermined'
2525 }
26- Else {
26+ else {
2727 $bottleneck = $bottleneck.Trim ()
2828 }
2929
@@ -40,7 +40,7 @@ Add-Type -AssemblyName System.Web
4040
4141
4242# Start logging if logging is enabled in config
43- If ($Config.logging.enabled ) {
43+ if ($Config.logging.enabled ) {
4444 # # Wait until log file is closed by Bootstrap.ps1
4545 try {
4646 $count = 1
@@ -79,23 +79,23 @@ try {
7979 $vbrSessionLogger = $session.Logger
8080
8181 # # Wait for the backup session to finish.
82- If ($session.State -ne ' Stopped' ) {
82+ if ($session.State -ne ' Stopped' ) {
8383 $nonStoppedStates = ' Idle' , ' Pausing' , ' Postprocessing' , ' Resuming' , ' Starting' , ' Stopping' , ' WaitingRepository' , ' WaitingTape ' , ' Working'
8484 $timeout = New-TimeSpan - Minutes 5
8585 $stopwatch = [System.Diagnostics.Stopwatch ]::StartNew()
86- Do {
86+ do {
8787 Write-LogMessage - Tag ' INFO' - Message ' Session not finished. Sleeping...'
8888 Start-Sleep - Seconds 10
8989 $session = (Get-VBRSessionInfo - SessionId $id - JobType $jobType ).Session
9090 }
91- While ($session.State -in $nonStoppedStates -and $stopwatch.elapsed -lt $timeout )
91+ while ($session.State -in $nonStoppedStates -and $stopwatch.elapsed -lt $timeout )
9292 $stopwatch.Stop ()
9393 }
9494
9595 # # Quit if still not stopped
96- If ($session.State -ne ' Stopped' ) {
96+ if ($session.State -ne ' Stopped' ) {
9797 Write-LogMessage - Tag ' ERROR' - Message ' Session not stopped. Aborting.'
98- Exit 1
98+ exit 1
9999 }
100100
101101 # Add Veeam session log entry.
@@ -137,7 +137,7 @@ try {
137137 $speedRound = (ConvertTo-ByteUnit - Data $speed ).ToString() + ' /s'
138138
139139 # Set processing speed "Unknown" if 0B/s to avoid confusion.
140- If ($speedRound -eq ' 0 B/s' ) {
140+ if ($speedRound -eq ' 0 B/s' ) {
141141 $speedRound = ' Unknown'
142142 }
143143
@@ -153,19 +153,19 @@ try {
153153 $sessionObjectFails = 0
154154
155155 foreach ($object in $sessionObjects) {
156- If ($object.Status -eq 'Warning') {
156+ if ($object.Status -eq 'Warning') {
157157 $sessionObjectWarns++
158158 }
159159 # TODO: check if 'Failed' is a valid state.
160- If ($object.Status -eq 'Failed') {
160+ if ($object.Status -eq 'Failed') {
161161 $sessionObjectFails++
162162 }
163163 }
164164 #>
165165
166166 <# TODO: utilise this.
167167 # Add object warns/fails to fieldArray if any.
168- If ($sessionObjectWarns -gt 0) {
168+ if ($sessionObjectWarns -gt 0) {
169169 $fieldArray += @(
170170 [PSCustomObject]@{
171171 name = 'Warnings'
@@ -174,7 +174,7 @@ try {
174174 }
175175 )
176176 }
177- If ($sessionObjectFails -gt 0) {
177+ if ($sessionObjectFails -gt 0) {
178178 $fieldArray += @(
179179 [PSCustomObject]@{
180180 name = 'Fails'
@@ -187,7 +187,7 @@ try {
187187 }
188188
189189 # If agent backup, gather and include session info.
190- If ($jobType -in ' EpAgentBackup' , ' BackupToTape' , ' FileToTape' ) {
190+ if ($jobType -in ' EpAgentBackup' , ' BackupToTape' , ' FileToTape' ) {
191191 # Gather session data sizes and timings.
192192 [Float ]$processedSize = $session.Info.Progress.ProcessedSize
193193 [Float ]$transferSize = $session.Info.Progress.TransferedSize
@@ -202,7 +202,7 @@ try {
202202 $speedRound = (ConvertTo-ByteUnit - Data $speed ).ToString() + ' /s'
203203
204204 # Set processing speed "Unknown" if 0B/s to avoid confusion.
205- If ($speedRound -eq ' 0 B/s' ) {
205+ if ($speedRound -eq ' 0 B/s' ) {
206206 $speedRound = ' Unknown'
207207 }
208208 }
@@ -218,7 +218,7 @@ try {
218218 $duration = $endTime - $startTime
219219
220220 # # Switch for job duration; define pretty output.
221- Switch ($duration ) {
221+ switch ($duration ) {
222222 { $_.Days -ge ' 1' } {
223223 $durationFormatted = ' {0}d {1}h {2}m {3}s' -f $_.Days , $_.Hours , $_.Minutes , $_.Seconds
224224 break
@@ -235,17 +235,17 @@ try {
235235 $durationFormatted = ' {0}s' -f $_.Seconds
236236 break
237237 }
238- Default {
238+ default {
239239 $durationFormatted = ' {0}d {1}h {2}m {3}s' -f $_.Days , $_.Hours , $_.Minutes , $_.Seconds
240240 }
241241 }
242242
243243 # Define nice job type name
244- Switch ($jobType ) {
244+ switch ($jobType ) {
245245 Backup { $jobTypeNice = ' VM Backup' }
246246 Replica { $jobTypeNice = ' VM Replication' }
247247 EpAgentBackup {
248- Switch ($session.Platform ) {
248+ switch ($session.Platform ) {
249249 ' ELinuxPhysical' { $jobTypeNice = ' Linux Agent Backup' }
250250 ' EEndPoint' { $jobTypeNice = ' Windows Agent Backup' }
251251 }
@@ -257,28 +257,28 @@ try {
257257 # Decide whether to mention user
258258 $mention = $false
259259 # # On fail
260- Try {
261- If ($Config.mentions.on_failure -and $status -eq ' Failed' ) {
260+ try {
261+ if ($Config.mentions.on_failure -and $status -eq ' Failed' ) {
262262 $mention = $true
263263 }
264264 }
265- Catch {
265+ catch {
266266 Write-LogMessage - Tag ' WARN' - Message " Unable to determine 'mention on fail' configuration. User will not be mentioned."
267267 }
268268
269269 # # On warning
270- Try {
271- If ($Config.mentions.on_warning -and $status -eq ' Warning' ) {
270+ try {
271+ if ($Config.mentions.on_warning -and $status -eq ' Warning' ) {
272272 $mention = $true
273273 }
274274 }
275- Catch {
275+ catch {
276276 Write-LogMessage - Tag ' WARN' - Message " Unable to determine 'mention on warning' configuration. User will not be mentioned."
277277 }
278278
279279
280280 # Define footer message.
281- Switch ($updateStatus.Status ) {
281+ switch ($updateStatus.Status ) {
282282 Current {
283283 $footerMessage = " tigattack's VeeamNotify $ ( $updateStatus.CurrentVersion ) - Up to date."
284284 }
@@ -288,14 +288,14 @@ try {
288288 Ahead {
289289 $footerMessage = " tigattack's VeeamNotify $ ( $updateStatus.CurrentVersion ) - Pre-release."
290290 }
291- Default {
291+ default {
292292 $footerMessage = " tigattack's VeeamNotify $ ( $updateStatus.CurrentVersion ) "
293293 }
294294 }
295295
296296
297297 # Build embed parameters
298- If ($jobType -in ' EpAgentBackup' , ' BackupToTape' , ' FileToTape' ) {
298+ if ($jobType -in ' EpAgentBackup' , ' BackupToTape' , ' FileToTape' ) {
299299 $payloadParams = @ {
300300 JobName = $jobName
301301 JobType = $jobTypeNice
@@ -334,10 +334,10 @@ try {
334334 }
335335
336336 # Add update message if relevant.
337- If ($config.update | Get-Member - Name ' notify' ) {
337+ if ($config.update | Get-Member - Name ' notify' ) {
338338 $config.update.notify = $true
339339 }
340- If ($updateStatus.Status -eq ' Behind' -and $config.update.notify ) {
340+ if ($updateStatus.Status -eq ' Behind' -and $config.update.notify ) {
341341 $payloadParams += @ {
342342 UpdateNotification = $true
343343 LatestVersion = $updateStatus.LatestStable
@@ -346,7 +346,7 @@ try {
346346
347347
348348 # Build embed and send iiiit.
349- Try {
349+ try {
350350 $Config.services.PSObject.Properties | ForEach-Object {
351351
352352 # Create variable from current pipeline object to simplify usability.
@@ -355,19 +355,19 @@ try {
355355 # Create variable for service name in TitleCase format.
356356 $textInfo = (Get-Culture ).TextInfo
357357 $serviceName = $textInfo.ToTitleCase ($service.Name )
358- If ($service.Value.webhook -ne $null ) {
359- If ($service.Value.webhook.StartsWith (' https' )) {
358+ if ($service.Value.webhook -ne $null ) {
359+ if ($service.Value.webhook.StartsWith (' https' )) {
360360 Write-LogMessage - Tag ' INFO' - Message " Sending notification to $ ( $serviceName ) ."
361361 $logId_service = $vbrSessionLogger.AddLog (" [VeeamNotify] Sending notification to $ ( $serviceName ) ..." )
362362
363363 # Add user information for mention if relevant.
364364 Write-LogMessage - Tag ' DEBUG' - Message ' Determining if user should be mentioned.'
365- If ($mention ) {
365+ if ($mention ) {
366366 Write-LogMessage - Tag ' DEBUG' - Message ' Getting user ID for mention.'
367367 $payloadParams.UserId = $service.Value.user_id
368368
369369 # Set username if exists
370- If ($service.Value.user_name -and $service.Value.user_name -ne ' Your Name' ) {
370+ if ($service.Value.user_name -and $service.Value.user_name -ne ' Your Name' ) {
371371 Write-LogMessage - Tag ' DEBUG' - Message ' Setting user name for mention.'
372372 $payloadParams.UserName = $service.Value.user_name
373373 }
@@ -376,40 +376,40 @@ try {
376376 # Get URI from webhook value
377377 $uri = $service.Value.webhook
378378
379- Try {
379+ try {
380380 New-Payload - Service $service.Name - Parameters $payloadParams | Send-Payload - Uri $uri - JSONPayload $true | Out-Null
381381
382382 Write-LogMessage - Tag ' INFO' - Message " Notification sent to $serviceName successfully."
383383 $vbrSessionLogger.UpdateSuccess ($logId_service , " [VeeamNotify] Sent notification to $ ( $serviceName ) ." ) | Out-Null
384384 }
385- Catch {
385+ catch {
386386 Write-LogMessage - Tag ' ERROR' - Message " Unable to send $serviceName notification: $_ "
387387 $vbrSessionLogger.UpdateErr ($logId_service , " [VeeamNotify] $serviceName notification could not be sent." , " Please check the log: $Logfile " ) | Out-Null
388388 }
389389 }
390- Else {
390+ else {
391391 Write-LogMessage - Tag ' DEBUG' - Message " $serviceName is unconfigured (invalid URL). Skipping $serviceName notification."
392392 }
393393 }
394394 else {
395395 # Get URI from webhook value
396- If ($service.Name -eq ' telegram' ) {
396+ if ($service.Name -eq ' telegram' ) {
397397 if (! ($Service.Value.bot_token -eq ' TelegramBotToken' -or $Service.Value.chat_id -eq ' TelegramChatID' )) {
398398 Write-LogMessage - Tag ' INFO' - Message " Sending notification to $ ( $serviceName ) ."
399399 $logId_service = $vbrSessionLogger.AddLog (" [VeeamNotify] Sending notification to $ ( $serviceName ) ..." )
400- Try {
400+ try {
401401 $payload = New-Payload - Service $service.Name - Parameters $payloadParams
402402 Send-Payload - Uri " https://api.telegram.org/bot$ ( $service.Value.bot_token ) /sendMessage" - Body @ { chat_id = " $ ( $service.Value.chat_id ) " ; parse_mode = ' MarkdownV2' ; text = $payload }
403403
404404 Write-LogMessage - Tag ' INFO' - Message " Notification sent to $serviceName successfully."
405405 $vbrSessionLogger.UpdateSuccess ($logId_service , " [VeeamNotify] Sent notification to $ ( $serviceName ) ." ) | Out-Null
406406 }
407- Catch {
407+ catch {
408408 Write-LogMessage - Tag ' ERROR' - Message " Unable to send $serviceName notification: $_ "
409409 $vbrSessionLogger.UpdateErr ($logId_service , " [VeeamNotify] $serviceName notification could not be sent." , " Please check the log: $Logfile " ) | Out-Null
410410 }
411411 }
412- Else {
412+ else {
413413 Write-LogMessage - Tag ' DEBUG' - Message " $serviceName is unconfigured (invalid bot_token or chat_id). Skipping $serviceName notification."
414414 }
415415 }
@@ -419,19 +419,19 @@ try {
419419 # Update Veeam session log.
420420 $vbrSessionLogger.AddSuccess (' [VeeamNotify] Notification(s) sent successfully.' ) | Out-Null
421421 }
422- Catch {
422+ catch {
423423 Write-LogMessage - Tag ' WARN' - Message " Unable to send notification(s): $_ "
424424 $vbrSessionLogger.AddErr (' [VeeamNotify] An error occured while sending notification(s).' , " Please check the log: $Logfile " ) | Out-Null
425425 }
426- Finally {
426+ finally {
427427 $vbrSessionLogger.RemoveRecord ($logId_notification ) | Out-Null
428428 }
429429
430430 # Clean up old log files if configured
431431 if ($Config.logging.max_age_days -ne 0 ) {
432432 Write-LogMessage - Tag ' DEBUG' - Message ' Running log cleanup.'
433433
434- If ($config.logging.level -eq ' debug' ) {
434+ if ($config.logging.level -eq ' debug' ) {
435435 $debug = $true
436436 }
437437 else {
@@ -442,15 +442,15 @@ try {
442442 }
443443
444444 # If newer version available...
445- If ($updateStatus.Status -eq ' Behind' ) {
445+ if ($updateStatus.Status -eq ' Behind' ) {
446446
447447 # Add Veeam session log entry.
448- If ($Config.update.notify ) {
448+ if ($Config.update.notify ) {
449449 $vbrSessionLogger.AddWarning (" [VeeamNotify] A new version is available: $ ( $updateStatus.LatestStable ) . Currently running: $ ( $updateStatus.CurrentVersion ) " ) | Out-Null
450450 }
451451
452452 # Trigger update if configured to do so.
453- If ($Config.update.auto_update ) {
453+ if ($Config.update.auto_update ) {
454454
455455 # Copy update script out of working directory.
456456 Copy-Item $PSScriptRoot \Updater.ps1 $PSScriptRoot \..\VDNotifs- Updater.ps1
@@ -469,7 +469,7 @@ catch {
469469}
470470finally {
471471 # Stop logging.
472- If ($Config.logging.enabled ) {
472+ if ($Config.logging.enabled ) {
473473 Stop-Logging
474474 }
475475}
0 commit comments