Search code examples
powershellveeam

Trouble with foreach logic


I'm trying to get a list of all virtual machines in all of my Veeam backup jobs. I wrote this

#Add Veeam snapin
Add-PSSnapin VeeamPSSnapin

#variables
$Masterlist = @()
$jobs = Get-VBRJob

foreach($job in $jobs) {

    $backupJobObjects = Get-VBRJobObject -Job $job 

    foreach($backupJobObject in $backupJobObjects) {
        $MyObject = New-Object PSObject -Property @{ Name = $backupJobObject.Name }
    }
    $Masterlist += $MyObject
} 

$Masterlist | sort-object -Property Name

but it only spits out data from one job (there are 5). I assume this is because of some logic error in the foreach loop but I'm not seeing it. Can anyone help?


Solution

  • As per the per the comment from 4c74356b41

    foreach($backupJobObject in $backupJobObjects) {
            $MyObject = New-Object PSObject -Property @{ Name = $backupJobObject.Name }
            $Masterlist += $MyObject    
    }
    

    Is how the last foreach loop should look