Export Veeam console job log with PowerShell

These days I investigated the possibility to export Veeam Backup&Replication (VBR) console job log programmatically with PowerShell. Read here how this can be done.
To start to use PowerShell with Veeam, import the Veeam PowerShell Plugin:
Add-PSSnapin VeeamPSSnapIn
And connect to VBR server:
Connect-VBRServer -Server localhost
Then, select the backup session of the job you want to get the logs from. To select the job session run latest, you can use a command like this:
$BackupSession = Get-VBRBackupSession -Name "Job 1*" | Sort-Object CreationTime | Select-Object -last 1
Here the name of the job is actually Job 1. But -Name
is the name of the running instance. This means it includes a postfix like (Incremental), therefore I use *

I use -Name
because this is much more faster than using | where {$_.jobname -eq "Job 1"}
When you have the Backup session for your job, run this to get the logs for each VM in this job.
foreach ($Task in ($BackupSession | Get-VBRTaskSession)) {
$Task.logger.getlog().updatedrecords | select @{N="VM"; e={$task.name}}, starttime, status, title | sort starttime
}

There is also a way to get the log of the job, not a single VM. This can be done by a slightly change in the loop:
foreach ($Task in ($BackupSession)) {
$Task.logger.getlog().updatedrecords | select @{N="VM"; e={$task.name}}, starttime, status, title | sort starttime
}

By the way, there is a hidden feature in the VBR console: If you want a (sometimes even more detailed) log including date/time, mark all lines, right-click, copy to clipboard and past in an editor.

Notes
- To help to select a job session, see this post. The script there shows jobs that ran at a specific time(interval).