Last Backup Result

PowerShell script exchange

Last Backup Result

Veeam Logoby rsarnold1s » Fri Sep 09, 2016 8:05 pm

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?
rsarnold1s
Influencer
 
Posts: 11
Liked: never
Joined: Thu May 05, 2016 10:33 pm

Re: Last Backup Result

Veeam Logoby tsightler » Fri Sep 09, 2016 8:32 pm

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.
tsightler
Veeam Software
 
Posts: 4772
Liked: 1740 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Last Backup Result

Veeam Logoby rsarnold1s » Fri Sep 09, 2016 10:53 pm

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
}


rsarnold1s
Influencer
 
Posts: 11
Liked: never
Joined: Thu May 05, 2016 10:33 pm

Re: Last Backup Result

Veeam Logoby tsightler » Sat Sep 10, 2016 12:33 pm 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
tsightler
Veeam Software
 
Posts: 4772
Liked: 1740 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Last Backup Result

Veeam Logoby rsarnold1s » Sat Sep 10, 2016 7:54 pm

Thank you!
rsarnold1s
Influencer
 
Posts: 11
Liked: never
Joined: Thu May 05, 2016 10:33 pm


Return to PowerShell



Who is online

Users browsing this forum: Bison and 7 guests