PowerShell script exchange
Post Reply
mikeely
Expert
Posts: 224
Liked: 69 times
Joined: Nov 07, 2016 7:39 pm
Full Name: Mike Ely
Contact:

Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by mikeely »

Our monitoring uses Get-VBRJob combined with $job.GetLastResult() for all our regular backup jobs. Like so many others, once we added agent jobs to the mix we started getting the fairly confusing 'This cmdlet is no longer supported for computer backup jobs. Use "Get-VBRComputerBackupJob" instead.' alert which sent me digging into the module we wrote for monitoring. Suppressing that error is trivial but now it occurs to me that we're not monitoring status of the ComputerBackupJob types with our current monitoring, obviously need to fix that.

Problem is, Get-VBRComputerBackupJob | gm shows me nothing that speaks to the last job status so I'm unsure how to monitor these for success/failure.
'If you truly love Veeam, then you should not let us do this :D' --Gostev, in a particularly Blazing Saddles moment
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by oleg.feoktistov » 2 people like this post

Hi Mike,

VBRComputerBackupJob class has no method for that, indeed.
Despite the fact that Get-VBRJob is no longer supported for agent jobs, you still can use it to retrieve last result. It's just that other info more relevant to agent jobs is not shown. Otherwise, the approach below could help, but it is also unofficial:

Code: Select all

$agentJob = Get-VBRComputerBackupJob
$agentResult = [Veeam.Backup.Core.CBackupJob]::FindLastSession($agentJob.Id).Result
Thanks,
Oleg
mikeely
Expert
Posts: 224
Liked: 69 times
Joined: Nov 07, 2016 7:39 pm
Full Name: Mike Ely
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by mikeely »

Hi Oleg,

Thanks, this is very helpful. To clarify my understanding, Get-VBRJob will still throw an alert for a failed agent job? Here's the current test we're running - I can always use your code in a second loop to add to $failedjobs if needed:

Code: Select all

    foreach ($job in (Get-VBRJob -WarningAction:Ignore | where {$_.BackupPlatform.Platform -ne 'ELinuxPhysical' -and $_.BackupPlatform.Platform -ne 'EEndPoint'})) {
        $jobname = $job.LogNameMainPart
    	$status = $job.GetLastResult()
        
        if ($status -eq "Failed") {
            $failedjobs += "$jobname"
        }
    }
'If you truly love Veeam, then you should not let us do this :D' --Gostev, in a particularly Blazing Saddles moment
mikeely
Expert
Posts: 224
Liked: 69 times
Joined: Nov 07, 2016 7:39 pm
Full Name: Mike Ely
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by mikeely »

Update:

Get-VBRJob does not work for Agent jobs.

This is working correctly, but I'd love to have an "official" method for monitoring agent backup jobs:

Code: Select all

    foreach ($cJob in (Get-VBRComputerBackupJob)) {
        $cJobName = $cJob.Name
        $cStatus = [Veeam.Backup.Core.CBackupJob]::FindLastSession($cJob.Id).Result
        
        if ($cStatus -eq "Failed") {
            $failedjobs += "$cJobName"
        }
    }
'If you truly love Veeam, then you should not let us do this :D' --Gostev, in a particularly Blazing Saddles moment
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by oleg.feoktistov » 2 people like this post

Ok, I haven't tested Get-VBRJob with failed agent jobs in a great while, but now I see why else it's not supported for them.

To be precise, $job.GetLastResult() is not official either :D Official is all we document in our helpcenter.
There is no official direct method, but you can still use filtering to query last job results with official cmdlets.

For regular jobs:

Code: Select all

$job = Get-VBRJob -Name 'Backup Job'
$lastSession = Get-VBRBackupSession | where {$_.JobId -eq $job.Id} 
$lastSession[0].Result
For agent jobs:

Code: Select all

$job = Get-VBRComputerBackupJob -Name 'Agent Backup'
$lastSession = Get-VBRComputerBackupJobSession | where {$_.JobId -eq $job.Id} 
$lastSession[0].Result
Thanks!
mikeely
Expert
Posts: 224
Liked: 69 times
Joined: Nov 07, 2016 7:39 pm
Full Name: Mike Ely
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by mikeely »

I appreciate your help. We've updated our monitoring script to the following. This simply returns null or a list of failed backup jobs which our monitoring system will then alert on appropriately. I'm honestly unsure about scoping in PS and have no clue whether I needed to create new vars for the second foreach loop or not so I scoped them by hand :D

Code: Select all

asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue

function Get-VeeamStatus {

    $failedjobs=@()

    # Traditional VMWare backup jobs
    foreach ($job in (Get-VBRJob -WarningAction:Ignore | where {$_.BackupPlatform.Platform -ne 'ELinuxPhysical' -and $_.BackupPlatform.Platform -ne 'EEndPoint'})) {
        $jobname = $job.LogNameMainPart
    	$status = $job.GetLastResult()
        
        if ($status -eq "Failed") {
            $failedjobs += "$jobname"
        }
    }

    # Linux agent jobs
    foreach ($cjob in (Get-VBRComputerBackupJob)) {
        $cjobname = $cjob.Name
        $cstatus = [Veeam.Backup.Core.CBackupJob]::FindLastSession($cjob.Id).Result
        
        if ($cstatus -eq "Failed") {
            $failedjobs += "$cjobname"
        }
    }

    if ( $failedjobs -ne $null ) {
        foreach ( $fail in $failedjobs ) {
            Write-Host $fail
        }
    }
}

export-modulemember -function Get-VeeamStatus
'If you truly love Veeam, then you should not let us do this :D' --Gostev, in a particularly Blazing Saddles moment
evilaedmin
Expert
Posts: 176
Liked: 30 times
Joined: Jul 26, 2018 8:04 pm
Full Name: Eugene V
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by evilaedmin »

$null needs to be on the left side of any equality comparison. in the current way you are comparing the unrolled value of each item in collection to $null, is that your intent?

It's a whole thing with powershell that often feels under documented. The issue below has more links within.

Edit: better link: https://github.com/PowerShell/PSScriptA ... ithNull.md
mikeely
Expert
Posts: 224
Liked: 69 times
Joined: Nov 07, 2016 7:39 pm
Full Name: Mike Ely
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by mikeely » 1 person likes this post

Ugh, seriously? So it'd be

Code: Select all

if ( $null -ne $failedjobs) {
correct?

In perl this would look like

Code: Select all

if ($failedjobs) {
and C would be

Code: Select all

if (pointer[i] != NULL)
(although C allows a LOT of ways to express this), in Java it would be

Code: Select all

if (failedjobs != null)
and so on. All of those basically read "if failedjobs is not null" which is how human beings think, rather than "if null does not equal failedjobs" as "correct" powershell would have it.

Leave it to Microsoft to continue doing everything backwards...
'If you truly love Veeam, then you should not let us do this :D' --Gostev, in a particularly Blazing Saddles moment
evilaedmin
Expert
Posts: 176
Liked: 30 times
Joined: Jul 26, 2018 8:04 pm
Full Name: Eugene V
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by evilaedmin » 1 person likes this post

The perl way might be appropriate for the simple ask here.

Code: Select all

$failedJobs = @()
if ($failedJobs) { $true } else { $false}
False
if (!$failedJobs) { $true } else { $false}
True
Since $failedJobs is being implicitly unrolled as empty.

But since $failedJobs is declared it's def. not null

Code: Select all

 $failedJobs.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
jhoughes
Veeam Vanguard
Posts: 279
Liked: 112 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Castle Rock, CO
Contact:

Re: Need GetLastResult() equivalent for Get-VBRComputerBackupJob

Post by jhoughes » 1 person likes this post

You can also get around this by just checking if the count of $failedJobs is equal to or greater than 1, rather than worrying about the variable being null.

Code: Select all

if ($failedJobs.count -ge 1) { }
Husband, Father, Solutions Architect, Geek Extraordinaire | @DenverVMUG, @AustinVMUG & @ATXPowerShell leader | VMware vExpert | Cisco Champion
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests