Reporting Veeam backups using PowerShell

Share on facebook
Share on linkedin
Share on twitter
Share on google
By Thomas Weekley

One of my many tasks recently has involved the need to create a daily report on all our backup jobs running on our Veeam Backup and Replication environment. There are several details which needed to be included in this report.

  • Name of the VM

  • Backup job name

  • Backup job description

  • Last completed date

  • Last result

  • Backup size

  • Total size

To achieve this, I have opted to use PowerShell as there is a convenient PowerShell Snap-in for Veeam. The first thing you will want to do is download and install this Snap-in onto your computer.

To do so, the first line of our script will be to import this snap-in.

Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue

Now that we have the Veeam snap-in added we can begin working with it. We can use the cmdlet “Get-VBRJob” to gather information about all of the jobs we have configured in Veeam. With just this command, we can get each job name and description.

So, what we want to do is store the output of this command into a variable.

Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$j = Get-VBRJob

Using the output from the above variable, we can now create the bones of our loop. As some of the information we’re interested in is specifically about the last session, we can add in some lines to get this session.

Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$j = Get-VBRJob
ForEach ($job in $j) {


                $s = $job.FindLastSession()

}

Next we want to eliminate any jobs which might have been created but not yet run.

Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$j = Get-VBRJob
ForEach ($job in $j) {


                $s = $job.FindLastSession()

                If ($s -ne $null) {

                }

}

Now that we are only dealing with the last session for backup jobs which have run before, we can begin pulling out some of the details we need.

Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$j = Get-VBRJob
ForEach ($job in $j) {


                $s = $job.FindLastSession()

                If ($s -ne $null) {

                                ForEach ($vm in $s.GetTaskSessions()) {

                                                $JobName = $job.name
                                                $Description = $job.description
                                                $VMName = $vm.name
                                                $LastResult = $vm.status
                                                $LastCompleted = $s.CreationTime


                                }

                }

}

Great, so we have been able to get most of the information we need. However, we still need to get the backup size and the total size. Unfortunately, we cannot get this information from the output of “Get-VBRJob”.

To get this information we need to add to our loops.

Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$j = Get-VBRJob
ForEach ($job in $j) {


                $s = $job.FindLastSession()

                If ($s -ne $null) {

                                ForEach ($vm in $s.GetTaskSessions()) {

                                                $JobName = $job.name
                                                $Description = $job.description
                                                $VMName = $vm.name
                                                $LastResult = $vm.status
                                                $LastCompleted = $s.CreationTime


                                                $Restore = Get-VBRBackup -Name $job.name
                                                $RestorePoints = $Restore.GetAllStorages() | Sort CreationTime -Descending
                                                $jobBackupSzize = 0
                                                $jobTotalSize = 0


                                                ForEach ($point in $RestorePoints) {

                                                                $JobBackupSize += [long]($point | Select-Object -ExpandProperty stats | Select -ExpandProperty Backupsize)

                                                                $JobTotalSize += [long]($point | Select-Object -ExpandProperty stats | Select -ExpandProperty DataSize)

                                                }

$jobBackupSize = [math]::Round(($jobBackupSize / 1024 / 1024 / 1024), 2)

                                               $jobDataSize = [math]::Round(($jobDataSize /1024 / 1024 / 1024), 2)

                                }

                }

}

The Get-VBRBackup cmdlet lets us interrogate the backup itself as opposed to just the job. The extra lines of the script we added above pulls out the backup information based on the name of the job we are currently processing in the ForEach loop.

Next we are gathering a list of all restore points for the backup, sorting them into descending order and then storing the output into an array.

We then use another ForEach loop to obtain the size of each restore point and to calculate the total size. Finally, we then do some maths against the backup size and total size to turn the number into gigabytes.

Now that our script is completed, you can go ahead and add in any extra bits of code you might want to display the obtained information and output it to somewhere.

In my implementation for example, I am taking the information, putting it into a formatted HTML table which is then being emailed to a list of recipients. Your implementation can output it however you wish though.

Get updated

Newsletter

Interested in becoming a part of our growing community?
Keep up with our events, best practices, news, tech articles and more.