PowerShell script exchange
Post Reply
mateo.chvs
Novice
Posts: 5
Liked: 1 time
Joined: Apr 24, 2025 2:02 pm
Full Name: Matéo Chevassu
Contact:

Return Actions of Backup Copy

Post by mateo.chvs »

Hello,

I have a problem, it’s been several days that I’ve been trying to create a script that returns the actions of the last backup copy. Here’s the script I’ve made so far, the problem is that it only works for normal backups. Here it is:

Code: Select all

$cred = Import-Clixml -Path "**************"
Connect-VBRServer -Server "******" -Credential $cred
Import-Module Veeam.Backup.PowerShell -ErrorAction SilentlyContinue
$jobName = Read-Host "Write the name of your backup copy"
$job = Get-VBRJob | Where-Object { $_.Name -eq $jobName }
if (-not $job) {
    $job = Get-VBRBackupCopyJob | Where-Object { $_.Name -eq $jobName }
}
if (-not $job) {
    Write-Host "No job with this name : '$jobName'"
    Disconnect-VBRServer
    return
}
$lastSession = $job.FindLastSession()
if (-not $lastSession) {
    Write-Host "No session for this job : '$jobName'"
    Disconnect-VBRServer
    return
}
if ($lastSession) {
    foreach ($task in $lastSession.GetTaskSessions()) {
        $task.Logger.GetLog().UpdatedRecords |
        Select-Object @{Name="VM"; Expression = { $task.Name }},
                      StartTime,
                      Status,
                      Title,
                      Action |
        Sort-Object StartTime |
        Format-Table -AutoSize
    }
}
Disconnect-VBRServer
If you have any other questions, feel free to ask. Thank you in advance for your help!
david.domask
Veeam Software
Posts: 2607
Liked: 610 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Return Actions of Backup Copy

Post by david.domask » 1 person likes this post

Hi mateo.chvs, welcome to the forums.

Backup Copies currently require a bit of special processing to do good reporting on and we'll need to use unsupported methods here.

Here's a simple script to do a similar report on all your Backup Copy Jobs:

Code: Select all

$bcjs = Get-VBRBackupCopyJob
$fullJobs = Get-VBRJob | Where-Object {$_.id -in $bcjs.id}
$bcjSessReport = @()
Foreach($fj in $fullJobs){
	$workerJob = $fj.GetWorkerJobs()
	$workerLastSess = @()
	Foreach($w in $workerJob){
		$workerLastSess += $w.FindLastSession()
	}
	Foreach($ws in $workerLastSess){
		$tSess = Get-VBRTaskSession -Session $ws
		Foreach($t in $tSess){
			$data = [PSCustomObject]@{
				Job_Name = $fj.Name
				Job_SessID = $ws.id
				Job_Sess_StartTimeUTC = $ws.CreationTimeUtc
				VM_Name = $t.Name
				VM_Task_StartTimeUTC = $t.Progress.StartTimeUtc
				VM_Task_Result = $t.Status
			}
		$bcjSessReport += $data	
		Remove-Variable data
		}
	}
}
Everything will be saved to the array $bcjSessReport.

Normally I wouldn't do a script like this with so many nested ForEach, but given that we need a special approach with the GetWorkerJobs() method, will cheat a little to make it easier.

But this example script shows how to retrieve the data for a backup copy:

1. Get the Backup Copy job ID with Get-VBRBackupCopyJob
2. Get "full" job objects with Get-VBRJob ($fullJobs variable, we populate with Id's from the BCJ array
3. Get the worker jobs, which actually perform the backup copy processing
4. Get the Worker job's most recent session with FindLastSession()
5. Pass the worker sessions to Get-VBRTaskSession -- you need to loop over the workers as you may have multiple workers for a Backup Copy
6. Loop over the Get-VBRTaskSession results as well as a job likely has multiple tasks to review

Give it a shot and see how it works for you and if you can manage your own script based on this.
David Domask | Product Management: Principal Analyst
mateo.chvs
Novice
Posts: 5
Liked: 1 time
Joined: Apr 24, 2025 2:02 pm
Full Name: Matéo Chevassu
Contact:

Re: Return Actions of Backup Copy

Post by mateo.chvs »

Thank you so much for your help, I tried your script and it gave me some good insights! However, I’d like to retrieve information on the exact duration of the jobs, the sizes of the files processed, and the transfer speeds. Just like in the Veeam interface:

4/25/2025 4:21:03 AM :: Backup copy for VM-Hostname started at 4/25/2025 4:21:03 AM
4/25/2025 4:21:03 AM :: VM-Hostname (1.6 GB) processing finished at 4/25/2025 4:23:18 AM: 624.2 MB transferred at 22 MB/s
4/25/2025 4:23:11 AM :: Backup copy for VM-Hostname started at 4/25/2025 4:23:11 AM
4/25/2025 4:23:11 AM :: VM-Hostname (15.1 GB) processing finished at 4/25/2025 4:38:39 AM: 7.8 GB transferred at 18 MB/s
4/25/2025 4:38:29 AM :: Backup copy for VM-Hostname started at 4/25/2025 4:38:29 AM
4/25/2025 4:38:29 AM :: VM-Hostname (1.4 GB) processing finished at 4/25/2025 4:40:29 AM: 624.7 MB transferred at 22 MB/s

Do you think it would be possible to retrieve such details from the job sessions?
Thanks again for your help!
david.domask
Veeam Software
Posts: 2607
Liked: 610 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Return Actions of Backup Copy

Post by david.domask »

Hi Mateo,

You're very welcome, and sure you can get the exact duration.

Most Session objects (those returned by Get-VBRSession/VBRBackupSession/VBRRestoreSession/VBRTaskSession) have both a CreationTime and EndTime property; I think a few have Duration property but I don't remember. I would check each object by piping it to Get-Member (gm for short), e.g.:

$sess = Get-VBRBackupSession -Name "some job"
$sess | gm

And just check what properties are available.

The individual VM processing stats are returned from Get-VBRTaskSession, and stored under the Progress property, including AvgSpeed statistic.

Check it out and then you can modify the script to your liking to add the new properties to the PSCustomObject
David Domask | Product Management: Principal Analyst
mateo.chvs
Novice
Posts: 5
Liked: 1 time
Joined: Apr 24, 2025 2:02 pm
Full Name: Matéo Chevassu
Contact:

Re: Return Actions of Backup Copy

Post by mateo.chvs »

Thank you for your valuable information and suggestions. I’ve tried to explore the additional properties you mentioned, and I’m getting closer to the information I need; I can now retrieve the durations. However, I’m still facing some difficulties in putting everything together for the full report, especially when it comes to extracting sizes and transfer speeds for each VM in a structured way.
Do you have any advice on how to efficiently combine all of this, or could you guide me further to simplify the script? I appreciate your help, and I think I’m almost there thanks to your guidance!
david.domask
Veeam Software
Posts: 2607
Liked: 610 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Return Actions of Backup Copy

Post by david.domask »

Hi Mateo, you're quite welcome, glad I could help.

Can you maybe show what you've tried so far and explain the difficulty you're facing? There are many ways to compile a report like you're discussing, and most of it involves making a PSCustomObject as is done in the example script above. The sizes and speed can be found under the Progress property, and you would just call it like is shown in the script above.

Share your script and explain where the challenge is and we can try to assist.
David Domask | Product Management: Principal Analyst
mateo.chvs
Novice
Posts: 5
Liked: 1 time
Joined: Apr 24, 2025 2:02 pm
Full Name: Matéo Chevassu
Contact:

Re: Return Actions of Backup Copy

Post by mateo.chvs »

Hello,

I finally managed to figure it out thanks to your advice, thanks again. There's just one last thing I don't understand — yesterday, when I ran this script, everything worked perfectly, but since today I'm getting an error message. The exact same script was working fine yesterday, and that's what I don't understand.

Code: Select all

$jobName = Read-Host "Write the name of a Backup Copy"

$bcjs = Get-VBRBackupCopyJob | Where-Object {$_.Name -eq $jobName}

if ($bcjs.Count -eq 0) {
    Write-Host "No Backup Copy with this name"
    exit
}

foreach ($fj in $bcjs) {
    $workerJob = $fj.GetWorkerJobs()
    $workerLastSess = @()

    foreach ($w in $workerJob) {
        $workerLastSess += $w.FindLastSession()
    }

    foreach ($ws in $workerLastSess) {
        $tSess = Get-VBRTaskSession -Session $ws
        foreach ($t in $tSess) {
            $vmName = $t.Name
            $readSize = $t.Progress.ReadSize
            $transferedSize = $t.Progress.TransferedSize
            $avgSpeed = $t.Progress.AvgSpeed
            $stopTimeUtc = $t.Progress.StopTimeUtc

            $formattedReadSize = [math]::Round($readSize / 1GB, 2)    # GB
            $formattedTransferedSize = [math]::Round($transferedSize / 1GB, 2) # GB
            $formattedAvgSpeed = [math]::Round($avgSpeed / 1MB, 2)     # MB/s

            Write-Host "$vmName ($formattedReadSize GB) processing finished at $stopTimeUtc : $formattedTransferedSize GB transferred at $avgSpeed MB/s"
            Write-Host "-------------------------------------"  
        }
    }
}

Disconnect-VBRServer
Here is the error message I'm getting:

Method invocation failed because [Veeam.Backup.PowerShell.Infos.VBRBackupCopyJob] does not contain a method named 'GetWorkerJobs'.
At C:\***********.ps1:23 char:5
+ $workerJob = $fj.GetWorkerJobs()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
david.domask
Veeam Software
Posts: 2607
Liked: 610 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Return Actions of Backup Copy

Post by david.domask » 1 person likes this post

Hi mateo,

Very glad you got it going.

For your error, looks like part of the script got dropped. For this script to work for Backup Copies, we need to use unsupported methods in v12 so your line:

$bcjs = Get-VBRBackupCopyJob | Where-Object {$_.Name -eq $jobName}

Needs to be updated.

$bcjLite = Get-VBRBackupCopyJob | Where-Object {$_.Name -eq $jobName}
$bcjs = Get-VBRJob | Where-Object {$bcjLite.Id -contains $_.id}

Get-VBRJob returns the CJob object which has the method GetWorkerJobs() the script requires.
David Domask | Product Management: Principal Analyst
mateo.chvs
Novice
Posts: 5
Liked: 1 time
Joined: Apr 24, 2025 2:02 pm
Full Name: Matéo Chevassu
Contact:

Re: Return Actions of Backup Copy

Post by mateo.chvs » 1 person likes this post

Thank you, I’ll be able to integrate this into my dashboard now.
Have a great day!
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests