PowerShell script exchange
Post Reply
LewisE
Lurker
Posts: 1
Liked: never
Joined: Dec 12, 2018 11:33 am
Full Name: Lewis Evelyn
Contact:

Output too many jobs?

Post by LewisE »

Hello All,

I have half created this, half taken parts from other scripts and the output I'm getting is far larger than I would expect?

It is outputting 1000+ results, I only have 4 Backup jobs, with 1 VM in each and 7 restore points, so the max result I would expect would be 28?

I suspect I have looped in the wrong place, but I couldn't work it out. Any ideas?

Code: Select all

Add-PSSnapin VeeamPSSnapin
$backups = get-vbrbackup | ? { $_.JobType -match "^(Endpoint)?Backup$" }
$jobs = Get-VBRJob

$table = @()
foreach ($backup in $backups) {
 $objects = $backup.GetObjectOibsAll()
 $storages = $backup.GetAllStorages()

 foreach ($object in $objects) {
        foreach ($storage in $storages) {
            
            If ($backup.info.jobtargethostid -match $storage.info.HostID) { 
                $storagePath = $storage.filepath
                $StorageBackupSize = $storage.Stats.BackupSize
            }
            else {
                $storagePath = $null
                $StorageBackupSize = $null
            }

            foreach ($job in $jobs) {

                If ($backup.info.jobtargethostid -match $job.info.targethostid) { 
                    $NextRun = $job | Get-VBRJobScheduleOptions | Select -ExpandProperty nextrun
                }
            
        
                $table += New-Object -TypeName psobject -Property @{
                    BackupName=$backup.Name;
                    ObjectName=$object.Name;
                    ObjectCount=$object.CountOfValidOibs;
                    ObjectCreationTime=$object.CreationTime
                    NextRun=$nextrun
                    StoragePath=$storagePath
                    StorageBackupSize=[math]::round($StorageBackupSize /1Gb, 3)
                }
            }
        }
    }
}

$table | Sort-Object -Property "BackupName" | 
    select @{n='Job Name';e={$_.BackupName}},
    @{n='Backup Name';e={$_.ObjectName}},
    @{n='Restore Points';e={$_.ObjectCount}},
    @{n='Last Backup Time';e={$_.ObjectCreationTime}},
    @{n='Next Run';e={$_.NextRun}},
    @{n='Storage Path';e={$_.StoragePath}},
    @{n='Storage Backup Size (GB)';e={$_.StorageBackupSize}} | fl 
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Output too many jobs?

Post by veremin »

Before we dig deeper into actual scripting, can you tell me what exactly you're trying to achieve, so that we can disregard unneeded parts? Thanks!
Vek17
Service Provider
Posts: 49
Liked: 15 times
Joined: May 29, 2018 8:42 pm
Contact:

Re: Output too many jobs?

Post by Vek17 »

So you definitely have some issues going on here.
Structure wise these are your loops

Code: Select all

foreach ($backup in $backups) {
    foreach ($object in $objects) {
        foreach ($storage in $storages) {
            foreach ($job in $jobs) {
                $table += Item
            }
        }
    }
}
This means for every backup (There should be 4 most likely) you are iterating though every restore point in that backup (7). For every restore point you are iterating through every storage in the backup which will most likely be the same number as the number of restore points (7). And for Every Storage you are iterating through every job (4).

If we do the math from here with assumed numbers we get 7*7*4 = 196 objects added for each backup. Since there are most likely 4 backups you should end up with 196*4 = 784 total objects added to $Table. This is almost certainly not what you intend.
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: Output too many jobs?

Post by jhoughes »

Adding my reply from Reddit here as well.

Your loops should be configured differently, you'd start with getting all jobs to report against, while you don't necessarily need to get all backups to start with. If you are going to get all jobs & backups, then I would still filter the jobs to also only select the matching JobType that you are looking for.

From that point, your jobs should be your outer loop, since that is really the core item you are trying to report against and is tied to everything else.

Then your next loop should be to get the backups associated with the job.

Your final inner loop would be where you get the storage files within each backup, then create your object to output to the pipeline.

A lot of this is personal preference, but if you construct it this way, each lookup only runs a query and returns the objects directly associated with your current object, therefore less overhead.
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 16 guests