PowerShell script exchange
Post Reply
rsarnold1s
Influencer
Posts: 11
Liked: never
Joined: May 05, 2016 10:33 pm
Contact:

Last Backup Result

Post by rsarnold1s »

I currently have the following syntax that shows me the last backup result for jobs.

Code: Select all

Add-PSSnapin VeeamPSSnapin
        $VbrJobs = Get-VBRJob | Sort-Object typetostring, name
        
        Foreach($Job in $VbrJobs)
        {
            $JobName = $Job.Name
            $Result = Get-VBRBackupSession | Where {$_.JobName -eq "$JobName"} | Sort Creationtime -Descending | Select -First 1
            $Result = $Result.Result
           

        }


The problem is that it takes a very long time to run. Is there a better way to get the last backup result for all my jobs?
tsightler
VP, Product Management
Posts: 6011
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Last Backup Result

Post by tsightler »

One obvious thing is that Get-VBRBackupSession is a very heavy command (retrieves every session for every job) and the script is running that once for every job, so simply moving that outside the loop would likely make a huge difference:

Code: Select all

Add-PSSnapin VeeamPSSnapin
        $VbrJobs = Get-VBRJob | Sort-Object typetostring, name
        $Sessions = Get-VBRBackupSession

        Foreach($Job in $VbrJobs)
        {
            $JobName = $Job.Name
            $Result = $Sessions | Where {$_.JobName -eq "$JobName"} | Sort Creationtime -Descending | Select -First 1
            $Result = $Result.Result
        }
So now there's only a single call to Get-VBRBackupSession instead of one call for every job. Even better from a performance perspective would be to just get rid of that command completely, and all the filter stuff and just use the Job method GetLastStatus() like this:

Code: Select all

Add-PSSnapin VeeamPSSnapin
        $VbrJobs = Get-VBRJob | Sort-Object typetostring, name
        
        Foreach($Job in $VbrJobs)
        {
            $JobName = $Job.Name
            $Result = $Job.GetLastResult()
        }
You could probably even get rid of the loop, but I wasn't sure what else you might be doing in there so I just left that part. Use the method to get the last result might be slightly more likely to break during version changes (for example if the method name changed), but that method has been there quite a while with no changes and it's way faster, even in my small lab environment, the original script took 60 seconds, moving the cmdlet outside of the loop took 11 seconds, and using GetLastResult() took only .15 seconds which is an impressive 400x faster and should scale mostly linearly as the number of jobs and sessions increase.
rsarnold1s
Influencer
Posts: 11
Liked: never
Joined: May 05, 2016 10:33 pm
Contact:

Re: Last Backup Result

Post by rsarnold1s »

Thanks tsightler, works much better! What I am doing is displaying backup info on a webpage as a table so someone could easily see info on the backup jobs. With my code below, is there anyway I can exclude Backup-Copy jobs from being displayed?

Code: Select all

$Session = New-PSSession -ComputerName VEEAM
    Invoke-Command -Session $Session -ScriptBlock{
        Add-PSSnapin VeeamPSSnapin
        $VbrJobs = Get-VBRJob | Sort-Object typetostring, name
        $Repos = Get-VBRBackupRepository
        
        Foreach($Job in $VbrJobs)
        {
            #Get Job Name
            $JobName = $Job.Name
            
            #Get VMs in Job
            $Objects = $Job.GetObjectsInJob()

            #Get Restore Points
            $RP = $Job.GetOptions().backupstorageoptions.retainCycles

            #Get Days
            $Day = $Job.ScheduleOptions.OptionsDaily.DaysSrv
            $Freq = $Job.ScheduleOptions.OptionsDaily.kind

            If($Freq -eq "Everyday")
            {
                $Day = "Everyday"
            }

            #Get Backup Repository Name
            $RepoTarget = $Job.FindTargetRepository()
            $RepoTarget = $RepoTarget.Name
            
            #Get VM Name    
            $VM = $Objects.Name

            #Get Last Backup
            $Backup = Get-VBRBackup | Where{$_.JobName -eq "$JobName"}
            $LastBackup = $Backup.LastPointCreationTime

            #Get Last Backup Result
            $Result = $Job.GetLastResult()
            If($Result -like "*Success*")
            {
                $Result = "<font color='green'>$Result</font>"
            }
            If($Result -like "*Warning*")
            {
                $Result = "<font color='orange'>$Result</font>"
            }
            If($Result -like "*Fail*")
            {
                $Result = "<font color='red'>$Result</font>"
            }


            #Get Backup Type
            $BackupType = $Job.BackupTargetOptions.Algorithm

            #Get Syntethic
            $Syntethic = $Job.BackupTargetOptions.TransformToSyntethicDays
                  
    
            $Body += "<tr>"
            $Body += "<td>$JobName</td>"
            $Body += "<td>$VM</td>"
            $Body += "<td>$RP</td>"
            $Body += "<td>$Day</td>"
            $Body += "<td>$RepoTarget</td>"
            $Body += "<td>$LastBackup</td>"
            $Body += "<td>$Result</td>"
            $Body += "<td>$BackupType</td>"
            $Body += "<td>$Syntethic</td>"
            $Body += "</tr>"


        }
    
    Return $Body
    Disconnect-PSSession -Session $Session
}


tsightler
VP, Product Management
Posts: 6011
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Last Backup Result

Post by tsightler » 1 person likes this post

Sure, the VBRJob object includes a property of JobType, which can be values like Backup, BackupSync, and Replica, so you can simply add a filter like this:

Code: Select all

$VbrJobs = Get-VBRJob | ?{$_.JobType -eq "Backup"} | Sort-Object typetostring, name
rsarnold1s
Influencer
Posts: 11
Liked: never
Joined: May 05, 2016 10:33 pm
Contact:

Re: Last Backup Result

Post by rsarnold1s »

Thank you!
Isaac Rainsford
Novice
Posts: 7
Liked: 1 time
Joined: Feb 05, 2018 2:20 am
Full Name: Isaac Rainsford
Contact:

Re: Last Backup Result

Post by Isaac Rainsford »

Just stumbled on this easier method.

$VJobs = Get-VBRJob
ForEach ($VJob in $VJobs) {
$result = $VJob.Info.LatestStatus
}

No need to use Get-VBRBackupSession at all ;)
veremin
Product Manager
Posts: 20282
Liked: 2257 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Last Backup Result

Post by veremin » 1 person likes this post

It's better to stick with .GetLastResult() dynamic method. Otherwise, you will need to re-assign $Job variable regularly within the script in order for static LatestStatus property to contain up-to-date result. Thanks.
Isaac Rainsford
Novice
Posts: 7
Liked: 1 time
Joined: Feb 05, 2018 2:20 am
Full Name: Isaac Rainsford
Contact:

Re: Last Backup Result

Post by Isaac Rainsford »

That's a fair point.
I guess it wouldn't matter if you were confident backup status wouldn't change when running the script.
E.g., script takes <10 secs to run.

Cheers
veremin
Product Manager
Posts: 20282
Liked: 2257 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Last Backup Result

Post by veremin » 1 person likes this post

Sure, but most of the scripts working with job result or status check the state of supposedly running job, that's why I think it's better to stick with dynamic methods instead of static properties. Thanks,
arun.kr
Service Provider
Posts: 25
Liked: never
Joined: Aug 16, 2014 1:13 am
Full Name: Arun Kumar
Contact:

Re: Last Backup Result

Post by arun.kr »

Hello Everyone

I am running a powershell script with job state, last backup result and last recovery point creation time (Last Backup) property. Output always have disabled job with latest timestamp. No matter for how long job is disabled. everyday "last backup" column has latest timestamp.

Job Name Job Type Job State Last Backup Result Last Backup Schedule
Jobname Backup Stopped Warning 9/27/2018 10:15:42 PM Disabled

There are some job that we run when customer ask to run. So can not delete them. I need this column in case anyone in my team disable the job and forget to enable it, another can take care of it.

Code: Select all

$jobs = Get-VBRJob 

foreach ($job in $jobs){ 

$RP = $job.GetLastBackup().LastPointCreationTime

#$State = $job.GetLastState()

$LastBackupresult = $job.GetLastResult()

$columns = New-Object psobject
$columns | Add-Member -MemberType NoteProperty -Name "Job Name" -Value $job.Name
$columns | Add-Member -MemberType NoteProperty -Name "Job Type" -Value $job.JobType
$columns | Add-Member -MemberType NoteProperty -Name "Job State" -Value $State
$columns | Add-Member -MemberType NoteProperty -Name "Last Backup Result" -Value $LastBackupresult
$columns | Add-Member -MemberType NoteProperty -Name "Last Backup" -Value $RP

#$schedule = $job.IsScheduleEnabled 

#if($schedule -eq "true"){$schedule = "Enabled"}

#else{$schedule = "Disabled"}

#$columns | Add-Member -MemberType NoteProperty -Name "Schedule" -Value $schedule

$details += $columns

}
thanks in advance.

Thanks
Arun
veremin
Product Manager
Posts: 20282
Liked: 2257 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Last Backup Result

Post by veremin »

Aren't those disabled backup copy jobs by any chance? Because I've tested the script on disabled backup copy jobs and haven't come across the same issue. Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests