diff --git a/AlertSender.ps1 b/AlertSender.ps1 index 12553fe..1ce9dc0 100644 --- a/AlertSender.ps1 +++ b/AlertSender.ps1 @@ -1,4 +1,5 @@ param( + [String]$JobId, [String]$SessionId, [String]$JobType, $Config, @@ -72,7 +73,7 @@ try { # Job info preparation ## Get the backup session. - $session = (Get-VBRSessionInfo -SessionId $SessionId -JobType $JobType).Session + $session = (Get-VBRSessionInfo -SessionId $SessionId -JobId $JobId -JobType $JobType).Session ## Initiate logger variable $vbrSessionLogger = $session.Logger @@ -84,7 +85,7 @@ try { do { Write-LogMessage -Tag 'INFO' -Message 'Session not completed. Sleeping...' Start-Sleep -Seconds 10 - $session = (Get-VBRSessionInfo -SessionId $SessionId -JobType $JobType).Session + $session = (Get-VBRSessionInfo -SessionId $SessionId -JobId $JobId -JobType $JobType).Session } while ($false -eq $session.Info.IsCompleted -and $stopwatch.Elapsed -lt $timeout) $stopwatch.Stop() diff --git a/Bootstrap.ps1 b/Bootstrap.ps1 index de39b81..19fbfbd 100644 --- a/Bootstrap.ps1 +++ b/Bootstrap.ps1 @@ -6,6 +6,32 @@ param ( [string]$SessionId = $null ) +function FinishBootstrap { + param( + [switch]$Failed + ) + + if ($config.logging.enabled) { + Stop-Logging + + if ($newLogfile) { + # Rename log file to include the job name. + try { + Rename-Item -Path $logFile -NewName "$(Split-Path $newLogfile -Leaf)" -ErrorAction Stop + } + catch { + Write-Output "ERROR: Failed to rename log file: $_" | Out-File $logFile -Append + } + } + } + if ($Failed) { + exit 1 + } + else { + exit 0 + } +} + # Import modules Import-Module Veeam.Backup.PowerShell -DisableNameChecking Import-Module "$PSScriptRoot\resources\Logger.psm1" @@ -19,7 +45,7 @@ if ($JobId -and -not $SessionId) { $SessionId = (Get-VBRBackupSession | Where-Object {$_.JobId -eq $JobId} | Sort-Object EndTimeUTC -Descending | Select-Object -First 1).Id.ToString() if ($null -eq $SessionId) { Write-Output 'ERROR: Failed to retrieve the session ID for the provided job ID. Please check the job ID.' - exit 1 + FinishBootstrap -Failed } } elseif ($SessionId -and -not $JobId) { @@ -27,7 +53,7 @@ elseif ($SessionId -and -not $JobId) { $JobId = (Get-VBRBackupSession -Id $SessionId | Sort-Object EndTimeUTC -Descending | Select-Object -First 1).JobId.ToString() if ($null -eq $JobId) { Write-Output 'ERROR: Failed to retrieve the job ID for the provided session ID. Please check the session ID.' - exit 1 + FinishBootstrap -Failed } } if ($JobId -and $SessionId) { @@ -91,19 +117,32 @@ else { # At time of writing, there is no alternative way to discover the job time. Write-LogMessage -Tag 'INFO' -Message 'Getting VBR job details' $job = Get-VBRJob -WarningAction SilentlyContinue | Where-Object {$_.Id.ToString() -eq $JobId} -if (!$job) { - # Can't locate non tape job so check if it's a tape job - $job = Get-VBRTapeJob -WarningAction SilentlyContinue | Where-Object {$_.Id.ToString() -eq $JobId} - $JobType = $job.Type +if ($job) { + $JobType = $job.JobType } else { - $JobType = $job.JobType + # Can't locate non-tape job so check if it's a tape job + Write-LogMessage -Tag 'DEBUG' -Message "Job with ID $JobId not found in VBR jobs. Checking for tape jobs." + $job = Get-VBRTapeJob -WarningAction SilentlyContinue | Where-Object {$_.Id.ToString() -eq $JobId} + if ($job) { + $JobType = $job.Type + } + else { + Write-LogMessage -Tag 'ERROR' -Message "Job with ID $JobId not found in tape jobs. Exiting." + FinishBootstrap -Failed + } } # Get the session information and name. Write-LogMessage -Tag 'INFO' -Message 'Getting VBR session information' -$sessionInfo = Get-VBRSessionInfo -SessionId $SessionId -JobType $JobType +try { + $sessionInfo = Get-VBRSessionInfo -SessionId $SessionId -JobId $JobId -JobType $JobType +} +catch { + Write-LogMessage -Tag 'ERROR' -Message "Failed to retrieve session information: $_" + FinishBootstrap -Failed +} $jobName = $sessionInfo.JobName $vbrSessionLogger = $sessionInfo.Session.Logger @@ -112,7 +151,7 @@ $vbrLogEntry = $vbrSessionLogger.AddLog('[VeeamNotify] Parsing job & session inf # Quit if job type is not supported. if ($JobType -notin $supportedTypes) { Write-LogMessage -Tag 'ERROR' -Message "Job type '$($JobType)' is not supported." - exit 1 + FinishBootstrap -Failed } Write-LogMessage -Tag 'INFO' -Message "Bootstrap script for Veeam job '$jobName' (job $JobId session $SessionId) - Session & job detection complete." @@ -129,6 +168,7 @@ $newLogfile = "$PSScriptRoot\log\$($date)-$($logJobName).log" # Build argument string for the alert sender script. $powershellArguments = "-NoProfile -File $PSScriptRoot\AlertSender.ps1", ` + "-JobId `"$JobId`"", ` "-SessionId `"$SessionId`"", ` "-JobType `"$JobType`"", ` "-Config `"$configRaw`"", ` @@ -149,18 +189,7 @@ try { catch { Write-LogMessage -Tag 'ERROR' -Message "Failed to launch AlertSender.ps1: $_" $vbrSessionLogger.UpdateErr($vbrLogEntry, '[VeeamNotify] Failed to launch Alert Sender.', "Please check the log: $newLogfile") | Out-Null - exit 1 + FinishBootstrap -Failed } -# Stop logging. -if ($config.logging.enabled) { - Stop-Logging - - # Rename log file to include the job name. - try { - Rename-Item -Path $logFile -NewName "$(Split-Path $newLogfile -Leaf)" -ErrorAction Stop - } - catch { - Write-Output "ERROR: Failed to rename log file: $_" | Out-File $logFile -Append - } -} +FinishBootstrap diff --git a/resources/VBRSessionInfo.psm1 b/resources/VBRSessionInfo.psm1 index 8b17a69..329b48d 100644 --- a/resources/VBRSessionInfo.psm1 +++ b/resources/VBRSessionInfo.psm1 @@ -9,7 +9,9 @@ function Get-VBRSessionInfo { [Parameter(Mandatory)][ValidateNotNullOrEmpty()] [string]$SessionId, [Parameter(Mandatory)][ValidateNotNullOrEmpty()] - [string]$JobType + [string]$JobType, + [Parameter(Mandatory)][ValidateNotNullOrEmpty()] + [string]$JobId ) # Import VBR module @@ -33,16 +35,16 @@ function Get-VBRSessionInfo { # to load in whatever's required to utilise the GetByOriginalSessionId method. # See https://forums.veeam.com/powershell-f26/want-to-capture-running-jobs-by-session-type-i-e-sobr-tiering-t75583.html#p486295 Get-VBRSession -Id $SessionId | Out-Null + # Get the session details. - $session = [Veeam.Backup.Core.CBackupSession]::GetByOriginalSessionId($SessionId) + $session = [Veeam.Backup.Core.CBackupSession]::GetByJob($JobId) | Select-Object -Last 1 - # Copy the job's name to it's own variable. - if ($JobType -eq 'EpAgentBackup') { - $jobName = $job.Info.Name - } - elseif ($JobType -in 'BackupToTape', 'FileToTape') { - $jobName = $job.Name + if ($null -eq $session) { + throw "$JobType job session with ID '$SessionId' could not be found." } + + # Extract the job name from the session + $jobName = $session.Name } }