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.