2

I have followed different posts on the web to fix this issue but my code still returns a single element insteaf of array. My code below:

Add-PSSnapin -Name VeeamPSSnapIn -WarningAction SilentlyContinue
    $sessionVMSummary = @()
    $bkJobs = get-vbrjob | foreach {
        $session = $_.findlastsession()
        if (($session -ne $NULL) -and ($_.isScheduleEnabled -eq $TRUE)) {
            # Get session details
            $sessionDocument = New-Object PSObject -Property @{
                "Name" = $session.JobName
                "Result" = $session.Result.toString()
                "ObjectStatus" = @()
            }
            [Veeam.Backup.Core.CBackupTaskSession]::GetByJobSession($session.id) | foreach {
                $Info = New-Object PSObject -Property @{
                    "Start Time" = $_.Progress.StartTime
                    "End Time" = $_.Progress.StopTime
                    "Duration" = $_.Progress.Duration
                }
                $sessionDocument.ObjectStatus += $Info
            }
            $sessionVMSummary += $sessionDocument
        }
    }

    return $sessionVMSummary

Question 1: How can I make $sessionVMSummary to return an array whith 1 element? Question 2: How can I make my code more efficient from the grammar point of view?

thanks

1 Answer 1

4

Use the comma operator to wrap your array in another array e.g.:

return ,$sessionVMSummary
Sign up to request clarification or add additional context in comments.

2 Comments

thank you @KeithHill. This will return an ArrayList and I need [Object]. The thing is ConvertTo-JSON of ArrayList will only return an object instead of an array... weird
Then just call ToArray() on the ArrayList e.g. return ,($sessionVMSummary.ToArray())

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.