diff --git a/Monitor-VeeamJobs.ps1 b/Monitor-VeeamJobs.ps1 index c872523..0959041 100644 --- a/Monitor-VeeamJobs.ps1 +++ b/Monitor-VeeamJobs.ps1 @@ -137,6 +137,41 @@ function Get-ObjectValue { return $null } +function Invoke-ObjectMethodValue { + param( + [Parameter(Mandatory = $true)] + [object]$InputObject, + + [Parameter(Mandatory = $true)] + [string[]]$Names + ) + + foreach ($name in $Names) { + if ($null -eq $InputObject) { + return $null + } + + $method = $InputObject.PSObject.Methods[$name] + + if ($null -eq $method) { + continue + } + + try { + $value = $method.Invoke() + + if ($null -ne $value) { + return $value + } + } + catch { + continue + } + } + + return $null +} + function Get-VeeamJobName { param( [Parameter(Mandatory = $true)] @@ -193,38 +228,95 @@ function Get-VeeamJobScheduleEnabled { ) } -function Get-VeeamJobLastResult { +function Get-VeeamJobLastSession { param( [Parameter(Mandatory = $true)] [object]$Job ) - return Get-ObjectValue -InputObject $Job -Paths @( + return Invoke-ObjectMethodValue -InputObject $Job -Names @( + 'FindLastSession', + 'GetLastSession', + 'FindLastBackupSession' + ) +} + +function Get-VeeamJobLastResult { + param( + [Parameter(Mandatory = $true)] + [object]$Job, + + [object]$LastSession + ) + + $value = Get-ObjectValue -InputObject $Job -Paths @( 'Info.LastResult', 'LastResult', 'Info.LatestRunResult', 'LatestRunResult' ) + + if ($null -ne $value) { + return $value + } + + $value = Invoke-ObjectMethodValue -InputObject $Job -Names @( + 'GetLastResult', + 'FindLastResult' + ) + + if ($null -ne $value) { + return $value + } + + if ($null -eq $LastSession) { + return $null + } + + return Get-ObjectValue -InputObject $LastSession -Paths @( + 'Result', + 'Info.Result', + 'JobResult', + 'Status' + ) } function Get-VeeamJobLastState { param( [Parameter(Mandatory = $true)] - [object]$Job + [object]$Job, + + [object]$LastSession ) - return Get-ObjectValue -InputObject $Job -Paths @( + $value = Get-ObjectValue -InputObject $Job -Paths @( 'Info.LastState', 'LastState', 'Info.LatestRunState', 'LatestRunState' ) + + if ($null -ne $value) { + return $value + } + + if ($null -eq $LastSession) { + return $null + } + + return Get-ObjectValue -InputObject $LastSession -Paths @( + 'State', + 'Info.State', + 'Status' + ) } function Get-VeeamJobLastRun { param( [Parameter(Mandatory = $true)] - [object]$Job + [object]$Job, + + [object]$LastSession ) $value = Get-ObjectValue -InputObject $Job -Paths @( @@ -240,6 +332,17 @@ function Get-VeeamJobLastRun { 'LastEndTime' ) + if ($null -eq $value -and $null -ne $LastSession) { + $value = Get-ObjectValue -InputObject $LastSession -Paths @( + 'CreationTime', + 'CreationTimeLocal', + 'EndTime', + 'EndTimeLocal', + 'Info.CreationTime', + 'Info.EndTime' + ) + } + return ConvertTo-ZabbixDateTime -Value $value } @@ -394,8 +497,9 @@ function Get-VeeamJobMetrics { $jobDetails = @($allJobs | ForEach-Object { $jobName = Get-VeeamJobName -Job $_ $scheduleEnabled = Get-VeeamJobScheduleEnabled -Job $_ - $lastResult = Get-VeeamJobLastResult -Job $_ - $lastState = Get-VeeamJobLastState -Job $_ + $lastSession = Get-VeeamJobLastSession -Job $_ + $lastResult = Get-VeeamJobLastResult -Job $_ -LastSession $lastSession + $lastState = Get-VeeamJobLastState -Job $_ -LastSession $lastSession $restorePoints = $null if ($restorePointCounts.ContainsKey($jobName)) { @@ -405,7 +509,7 @@ function Get-VeeamJobMetrics { [ordered]@{ name = $jobName enabled = $scheduleEnabled - last_run = Get-VeeamJobLastRun -Job $_ + last_run = Get-VeeamJobLastRun -Job $_ -LastSession $lastSession next_run = Get-VeeamJobNextRun -Job $_ last_result = if ($null -eq $lastResult) { $null } else { [string]$lastResult } last_state = if ($null -eq $lastState) { $null } else { [string]$lastState } @@ -418,21 +522,21 @@ function Get-VeeamJobMetrics { $scheduleEnabled -eq $false }) $errorJobs = @($allJobs | Where-Object { - $lastState = Get-VeeamJobLastState -Job $_ - $lastResult = Get-VeeamJobLastResult -Job $_ + $lastSession = Get-VeeamJobLastSession -Job $_ + $lastState = Get-VeeamJobLastState -Job $_ -LastSession $lastSession + $lastResult = Get-VeeamJobLastResult -Job $_ -LastSession $lastSession $lastState -eq 'Failed' -or $lastResult -eq 'Failed' }) $warningJobs = @($allJobs | Where-Object { - $lastState = Get-VeeamJobLastState -Job $_ - $lastResult = Get-VeeamJobLastResult -Job $_ + $lastSession = Get-VeeamJobLastSession -Job $_ + $lastState = Get-VeeamJobLastState -Job $_ -LastSession $lastSession + $lastResult = Get-VeeamJobLastResult -Job $_ -LastSession $lastSession $lastState -eq 'Warning' -or $lastResult -eq 'Warning' }) - $successfulJobs = @($allJobs | Where-Object { - $lastResult = Get-VeeamJobLastResult -Job $_ - - $lastResult -eq 'Success' + $successfulJobs = @($jobDetails | Where-Object { + $_.last_result -in @('Success', 'Succeeded') -or $_.last_state -in @('Success', 'Succeeded') }) $disabledCount = $disabledJobs.Count