PowerShell script exchange
Post Reply
audrey
Novice
Posts: 4
Liked: 1 time
Joined: Nov 10, 2023 10:25 am
Full Name: Jenny R Wing
Contact:

find last *stopped* sessions

Post by audrey » 1 person likes this post

Please bear with me, this is not my usual field.

I need regularly – many times a day – to evaluate all last backup sessions. I am currently doing it like this:

Code: Select all

    Try {
        $backupSessions1 = [Veeam.Backup.Core.CBackupJob]::GetAll().findLastSession()
    } Catch {}
    Try {
        # only read tape sessions to avoid duplicates
        $backupSessions2 = ([Veeam.Backup.Core.CBackupJob]::GetAllBackupSyncAndTape() | Where {$_.TypeToString -Eq "Backup to Tape"}).findLastSession()
    } Catch {}
    $combinedSessions = @()
    if ($backupSessions1 -ne $null) {
        $combinedSessions += $backupSessions1
    }
    if ($backupSessions2 -ne $null) {
        $combinedSessions += $backupSessions2
    }
    
    # . . .
This relatively quickly returns a nice list of all backups. However, I am only interested in finished backups, not running ones.

Is there maybe a parameter to findlastSession() that could be used? Looping through all past backups and dynamically building a list is taking much too long.

Who has ideas?

Jenny
oleg.feoktistov
Veeam Software
Posts: 1918
Liked: 636 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: find last *stopped* sessions

Post by oleg.feoktistov »

Hi Jenny,

Try FindLastCompletedSession() method instead of FindLastSession().

Best regards,
Oleg
audrey
Novice
Posts: 4
Liked: 1 time
Joined: Nov 10, 2023 10:25 am
Full Name: Jenny R Wing
Contact:

Re: find last *stopped* sessions

Post by audrey »

Hi Oleg,

thank you very much for your input, but this does not seem to exist:

Code: Select all

$backupSessions1 = [Veeam.Backup.Core.CBackupJob]::GetAll().FindLastCompletedSession()
Fehler beim Aufrufen der Methode, da [Veeam.Backup.Core.CBackupJob] keine Methode mit dem Namen "findLastCompletedSession" enthält.
In Zeile:1 Zeichen:1
+ $backupSessions1 = [Veeam.Backup.Core.CBackupJob]::GetAll().FindLastC ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: find last *stopped* sessions

Post by david.domask »

Hi @audrey,

Your current script, while the logic is sound and correct commands, uses unsupported methods.

Try this script: it's a little slower than the unsupported .NET calls but I believe should work on v11 and forward:

Code: Select all

$JobTypes = '0','1','2','24','28','65'
$CurrDate = Get-Date
Foreach($jt in $JobTypes){
	$Last24hSess += Get-VBRSession -Type $jt | Where-Object {$_.CreationTime -gt $CurrDate.AddDays(-1) -and $_.CreationTme -lt $CurrDate} | Sort-Object -Property CreationTime -Descending
	}
Foreach($ls in $Last24hSess){
	Get-VBRBackupSession -Id $ls.id
	}

Job Name             State      Start Time             End Time               Result
--------             -----      ----------             --------               ------
vmware-per-job (I... Stopped    11/13/2023 8:00:09 AM  11/13/2023 8:01:52 AM  Warning
vmware-per-job (I... Stopped    11/13/2023 12:00:04 AM 11/13/2023 12:01:49 AM Warning
vmware-direct-obj... Stopped    11/12/2023 10:00:20 PM 11/12/2023 10:46:00 PM Warning
vmware-per-job (I... Stopped    11/12/2023 4:00:20 PM  11/12/2023 4:02:04 PM  Warning
repo-object-test     Stopped    11/13/2023 2:27:08 PM  11/13/2023 2:27:38 PM  Success
gfs                  Waiting... 11/13/2023 2:27:08 PM  1/1/1900 12:00:00 AM   None
tape-ps-test         Stopped    11/13/2023 2:27:08 PM  11/13/2023 2:27:12 PM  Success
test-job             Waiting... 11/12/2023 10:00:20 PM 1/1/1900 12:00:00 AM   None
vmware-per-job (I... Stopped    11/13/2023 8:00:09 AM  11/13/2023 8:01:52 AM  Warning
vmware-per-job (I... Stopped    11/13/2023 12:00:04 AM 11/13/2023 12:01:49 AM Warning
vmware-direct-obj... Stopped    11/12/2023 10:00:20 PM 11/12/2023 10:46:00 PM Warning
vmware-per-job (I... Stopped    11/12/2023 4:00:20 PM  11/12/2023 4:02:04 PM  Warning
repo-object-test     Stopped    11/13/2023 2:27:08 PM  11/13/2023 2:27:38 PM  Success
gfs                  Waiting... 11/13/2023 2:27:08 PM  1/1/1900 12:00:00 AM   None
tape-ps-test         Stopped    11/13/2023 2:27:08 PM  11/13/2023 2:27:12 PM  Success
test-job             Waiting... 11/12/2023 10:00:20 PM 1/1/1900 12:00:00 AM   None
vmware-per-job (I... Stopped    11/13/2023 8:00:09 AM  11/13/2023 8:01:52 AM  Warning
vmware-per-job (I... Stopped    11/13/2023 12:00:04 AM 11/13/2023 12:01:49 AM Warning
vmware-direct-obj... Stopped    11/12/2023 10:00:20 PM 11/12/2023 10:46:00 PM Warning
vmware-per-job (I... Stopped    11/12/2023 4:00:20 PM  11/12/2023 4:02:04 PM  Warning
repo-object-test     Stopped    11/13/2023 2:27:08 PM  11/13/2023 2:27:38 PM  Success
gfs                  Waiting... 11/13/2023 2:27:08 PM  1/1/1900 12:00:00 AM   None
tape-ps-test         Stopped    11/13/2023 2:27:08 PM  11/13/2023 2:27:12 PM  Success
test-job             Waiting... 11/12/2023 10:00:20 PM 1/1/1900 12:00:00 AM   None
You can see the output on the bottom and you can push the results of the second Foreach loop into some other array (declare it like $SessionData24h = @() before hand, then you can just use += within the loop to add to that array ($SessionData24h += {your code block to filter the sesssion further without brackets} )

The JobTypes are the numeric representation of the -Type flag on Get-VBRSession, but you can pass plaintext names from the Get-Help Get-VBRSession results and see the types of jobs you want to fetch. Just add them to the $JobTypes array.
David Domask | Product Management: Principal Analyst
audrey
Novice
Posts: 4
Liked: 1 time
Joined: Nov 10, 2023 10:25 am
Full Name: Jenny R Wing
Contact:

Re: find last *stopped* sessions

Post by audrey »

Thank you, David,

for your proposal. But I'll be needing to get a list of all last backups, not only the ones during the last 24 hours or similar.

It seems unfortunate to me that something like FindLastCompletedSessions() does not exist. Well.

Kind regards, Jenny
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: find last *stopped* sessions

Post by david.domask »

Hi audrey!

Is it expected you'll have backups that run irregularly and would need to be "caught" like that?

You can simply remove the date filters from what I have and set up a filter by piping to Where-Object and filtering on your JobIDs. I'm afraid there's not a supported way to do _just_ last session for each job, and I don't really want to recommend unsupported methods as you'll likely face similar issues on each release (as the unsupported methods change frequently which is one of the reasons that the methods are unsupported -- they're not meant to be relied on for scripting purposes)

Why not just then remove the filter, make a list of your JobIDs (get the jobID's by running the Get-VBRJob and Get-VBRTapeJob cmdlets, note the ID value for each, and make an array similar to what I did for the $JobTypes array)

Basically I'm not quite getting how your jobs are aligned, but as I get it it's not expected to have them run at least once every 24 hours? I'm afraid without more details on your setup it will be hard to describe a method that meets your reporting needs, and as noted, while with unsupported methods this is possible, as you can see the unsupported methods must not be relied on. The reason I'm guessing Oleg's suggestion didn't work is probably you're either trying to do this on an object without that unsupported method, or you're on version that doesn't have that method. On v12 CP3 it works:

PS C:\Users\Administrator> $jobs.FindLastCompletedSession()

Job Name State Start Time End Time Result
-------- ----- ---------- -------- ------
vmware-ffi-cap-bb... Stopped 11/6/2023 11:55:31 AM 11/6/2023 11:58:00 AM Success
ps-exclude-test (... Stopped 11/6/2023 11:57:43 AM 11/6/2023 12:00:18 PM Success
File Backup Job 1 Stopped 10/17/2023 7:34:32 PM 10/17/2023 7:34:56 PM Failed
File Backup Job 1... Stopped 11/10/2023 1:59:55 PM 11/10/2023 2:01:09 PM Failed
bmr-lhr-test Stopped 11/2/2023 5:26:02 PM 11/2/2023 5:33:28 PM Success
gfs-direct-test (... Stopped 9/25/2023 5:03:48 PM 9/25/2023 5:10:26 PM Warning
gfs-direct-test_c... Stopped 9/25/2023 5:41:05 PM 9/25/2023 5:47:43 PM Warning
vmware-per-job (I... Stopped 11/13/2023 4:00:06 PM 11/13/2023 4:01:51 PM Warning
cifs-lhr-test (Full) Stopped 11/7/2023 4:38:06 PM 11/7/2023 4:39:51 PM Failed
bfss-fix Stopped 10/31/2023 4:45:06 PM 10/31/2023 4:47:26 PM Failed
vmware-direct-obj... Stopped 11/12/2023 10:00:20 PM 11/12/2023 10:46:00 PM Warning
cap-tier-example ... Stopped 10/17/2023 12:48:35 PM 10/17/2023 12:51:29 PM Failed


I won't be able to tell you when this method appeared and as noted, I would not rely on it anyways.

Alternatively, try filtering the session results on WillBeRetried and IsWorking properties -- both should be False if the job is done.
David Domask | Product Management: Principal Analyst
audrey
Novice
Posts: 4
Liked: 1 time
Joined: Nov 10, 2023 10:25 am
Full Name: Jenny R Wing
Contact:

Re: find last *stopped* sessions

Post by audrey »

Hi,

our use case is generating a Prometheus metrics report for all scheduled backup jobs to be monitored with a Grafana/Alertmanager system. Our setup also include backups like quarterly jobs, and they must be monitored in the system as well.

The script shall check two things:

1. Are all scheduled backups successful
2. Are all scheduled backups recent enough

As the exporter script is running every few minutes, the script has to work relatively fast.
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: find last *stopped* sessions

Post by david.domask »

got it, but can I ask, does it really need updates every few minutes for backups? How often are you running backups?

Regarding the quarterly backups, how does the system handle the displaying if there aren't new sessions imported? That is, let's say I do a backup one day and that backup is reported by the script.

The script runs the next day and the backup has _not_ run -- what will your prometheus report show? The last backup time? Some error?
David Domask | Product Management: Principal Analyst
AndyBG
Novice
Posts: 3
Liked: 1 time
Joined: Oct 05, 2022 5:35 pm
Full Name: Andy G
Contact:

Re: find last *stopped* sessions

Post by AndyBG »

audrey wrote: Nov 13, 2023 3:35 pm As the exporter script is running every few minutes, the script has to work relatively fast.
Have you looked at using PowerShell & the VBR Rest API?

In my experience it is orders of magnitude faster than the Veeam PowerShell cmdlet's (in larger/busy environments). If you craft the query parameters for the timeframe and state you are looking for, I bet it will return in seconds.

https://helpcenter.veeam.com/docs/backu ... llSessions

I will admit, using a newer pwsh v7.3+ (even 7.4-rc) is by far preferable when using REST calls for ease of certificate/header/statuscode handling.
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests