PowerShell script exchange
Post Reply
Pezz
Novice
Posts: 3
Liked: never
Joined: Feb 17, 2023 11:36 am
Full Name: Perry
Contact:

Backup task reports

Post by Pezz »

Hi All,

Trying to create a simple PS script which displays the list of VMs through all Backup Jobs as well as each VMs most recent backup date. Going round in loops at the moment so any help would be very much appreciated, thank you in advance!
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Backup task reports

Post by david.domask » 2 people like this post

Heya @Pezz,

Try saving the result of Get-VBRBackup and passing the object to Get-VBRRestorePoint.

COib object CreationTime property should reflect what you're looking for, and you can write some logic to compare the restore points to some desired date/time and warn on objects that don't have a restore point in that time frame.

It might make sense to first:
0. Make some empty array for Results (e.g. $Results = @() )
1. Get the backups with Get-VBRBackup and save it to some variable
2. Get the jobs with Get-VBRJob and save it to some variable
3. Loop over the Backup object; fetch the job by finding the CBackup object's JobID property* and then get the CJob object from your Jobs variable like: $bJob = $jobs | Where-Object {$_.id -eq $backup.JobID}
4. For each backup object, get the restorepoints with Get-VBRRestorePoint -Backup $backup | Sort-Object -Property CreationTime, Name -Descending #note: I use -Descending because I like to start from most recent and work my way down, but that's just personal preference)
5. Write the desired properties to some PSCustomObject and add to $Results

* A small note; you might get Imported/Offloaded/other JobID sometimes with this, so you can filter out any 0'd UUID you find. I'm not sure on other edge cases you need to consider since it's highly dependent on what you're doing in your VBR.

Give it a shot and see what you come up with, we can help hammer it out.
David Domask | Product Management: Principal Analyst
Pezz
Novice
Posts: 3
Liked: never
Joined: Feb 17, 2023 11:36 am
Full Name: Perry
Contact:

Re: Backup task reports

Post by Pezz »

Hi David,

Thank you very much! I have created the below and all is working well however I am trying to just get the backups that were created in the last 7 days, the current script is displaying all backups. I attempted to restrict this but have obviously done something wrong somewhere.

Code: Select all

$Results = @()

$backups = Get-VBRBackup

$jobs = Get-VBRJob | Where-Object {$_.findlastsession().progress.stoptime -ge (Get-Date).AddDays(-7)}

foreach ($backup in $backups) {
    $bJob = $jobs | Where-Object { $_.id -eq $backup.JobID }

    $restorePoints = Get-VBRRestorePoint -Backup $backup | Sort-Object -Property CreationTime, Name -Descending

    foreach ($restorePoint in $restorePoints) {
        $result = [PSCustomObject]@{
            BackupName = $backup.Name
            RestorePointName = $restorePoint.Name
            CreationTime = $restorePoint.CreationTime           
        }
        $Results += $result
    }
}

$Results | Format-Table
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Backup task reports

Post by david.domask »

Happy to help @Pezz :)

A few items:

> $jobs = Get-VBRJob | Where-Object {$_.findlastsession().progress.stoptime -ge (Get-Date).AddDays(-7)}

FindLastSession() only returns the latest session, so all jobs are going to be returned by this unless they've not run within the last 7 days.

I think you want to move that filter on the Get-VBRRestorePoints and filter on the CreationTime property.

Where-Object {$_.CreationTime -ge $CheckDate}

I would set up your desired date and save it to $CheckDate outside of the loop just to avoid constantly calling it (not like it's expensive, but you might just get some weird oddities depending on when the script runs. I advise set it like:

$CheckDate = (Get-Date -hour 0 -minute 0 -second 0).AddDays(-7)

Does that help you get your desired check closer?
David Domask | Product Management: Principal Analyst
Pezz
Novice
Posts: 3
Liked: never
Joined: Feb 17, 2023 11:36 am
Full Name: Perry
Contact:

Re: Backup task reports

Post by Pezz »

Hi All,

Apologies if I should be opening a new thread for this but thought I would start here as it's relevant.

I currently have the script working as below. I am just looking to make a QoL change, currently it only shows successful jobs, is it possible to alter this script so that it can show failed jobs also and save the "Result" of a backup to a variable that I can then add to the PS table and create a column for "Backup result"?

Code: Select all

import-module veeam.backup.powershell -erroraction silentlycontinue

connect-VBRServer -server localhost

$datetime = ((get-date).ToString("MMMddyyyy-hhmmtt"))

$HTMLpath = "C:\Veeam backup reports\VeeamReport_$datetime.html"

$CSVpath = "C:\Veeam backup reports\VeeamReportCSV_$datetime.csv"

$Results = @()

$ReportingDate = (Get-Date).AddDays(-7)

$backups = Get-VBRBackup

$jobs = Get-VBRJob

foreach ($backup in $backups) {
    $bJob = $jobs | Where-Object { $_.id -eq $backup.JobID }

    $restorePoints = Get-VBRRestorePoint -Backup $backup | Where-Object {$_.CreationTime -ge $ReportingDate} | Sort-Object -Property CreationTime, Name -Descending

    foreach ($restorePoint in $restorePoints) {
        $result = [PSCustomObject]@{
            BackupName = $backup.Name
            RestorePointName = $restorePoint.Name
            CreationTime = $restorePoint.CreationTime           
        }
        $Results += $result
    }
}

$Results | Format-Table

$Results | out-file -FilePath $CSVpath

Disconnect-VBRServer
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Backup task reports

Post by david.domask »

Hi @Pezz,

No worries, I guess it makes sense to keep it in one spot.

Right now you're reporting on successful backups.

If you want a list of failed backup sessions, you'll need to parse the results of Get-VBRBackupSession

https://helpcenter.veeam.com/docs/backu ... ml?ver=120

There's not a direct relationship between sessions and backups, so I think the easiest way is to just have a code block that parses the Backup Job Sessions. The resulting object will have a property WillBeRetried and IsRetryMode; you can check each failed session for those properties and if IsRetryMode == $true and WillBeRetried == $false, it means the job reached the end of its retries and was not successful, so you can assume there was no backup for that job run.
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests