PowerShell script exchange
Post Reply
g0nzo
Influencer
Posts: 12
Liked: 2 times
Joined: Nov 09, 2019 4:28 pm
Contact:

Help with backup status report to csv

Post by g0nzo »

Hello,

I'm trying to get a PS report to show the backup jobs for the past 1 day and send to CSV, I have this that works only to output to the screen, but I was hoping to also select the extra attributes for objects, target, next run and description, does anyone know what these might be?

Code: Select all

Get-VBRBackupSession | ? { ( $_.CreationTime -ge (Get-Date).AddDays(-1) )} | Select JobName, JobType, CreationTime, Result, @{Name="BackupSize";Expression={$_.BackupStats.BackupSize}} | Sort CreationTime | Format-Table
Also does anyone have any useful PS scripts to show the licensing, backup proxy and backup repository info to CSV?

The reason I ask I need to get this into a DB then to Grafana.

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

Re: Help with backup status report to csv

Post by david.domask » 1 person likes this post

Hey g0nzo,

Sure, lemme show you how to do this a bit more cleanly; one liners are okay, but you're asking for info from many different objects, so you need a PSCustomObject (else you get an ugly one-line of golf code...cute trick but impossible to work with.

I actually am not sure if you want all retries reported also or not, but in fact I'd advise against it, just messy. Get-VBRSession is pretty slow because it potentially works with a LOT of sessions even within one day, so instead do it like this (hacking this out quickly so take the time to play with this a bit to get the format you like)

Edit: fixed a small typo bug, just forgot to call a method and had extra letters.

Code: Select all

using namespace System.Collections.Generic
$jobs = Get-VBRJob
$LastDay =  [List[Object]]@()
foreach($j in $jobs){
     $sess = $j.FindLastSession()
     $objects = Get-VBRJobObject -Job $j
     $JobLastRunDeets = [PSCustomObject]@{
          Name = $j.Name
          Type = $j.JobType
          StartTime = $sess.CreationTime
          Result = $sess.Result
          TargetRepo = $j.GetTargetRepository().Name
          BackupSize = $sess.BackupStats.BackupSize/1024/1024/1024
          }
     $LastDay.Add($JobLastRunDeets)
}
The output is something like the below when you pipe $LastDay | Export-CSV -NoTypeInformation -Path C:\some\path\some.csv:

Code: Select all

"Name","Type","StartTime","Result","TargetRepo","BackupSize"
"vmwware-gfs","Backup","3/10/2022 13:16:09","Warning",,"0.00154495239257813"
"offloadbackup","Backup","3/10/2022 22:18:08","Failed",,"0"
"pstesting","BackupSync","3/10/2022 23:16:20","None",,"0"
"vmware-fi-nixsobr","Backup","3/10/2022 11:34:01","Success",,"0.00154495239257813"
Ignore the part about using Namespace and the [List[Object]]@() part, just please accept this is a better way to do it than += as you see a lot :D (Also ignore the values for the backup size, my lab tests are done with tiny 500 MiB thin disks, so the value in GiB is really tiny!

Basically you can see that the bulk of this is building a PSCustomObject. You do that by just:

$someVar = [PSCustomObject]@{
SomeName = SomeValue
}

Make as many Names = Value combos as you like.

Because you want both session and Job data, and because Get-VBRJob is so much faster, we make a job array instead and then use the FindLastSession() method to get the same result. If you want to track retries, I believe the property IsRetryMode should give you what you want; if this is $true, then you know the job had a failure and you can flag it appropriately.

Play with the above and see what you want to be outputted.

Job Objects can be gotten with the cmdlet Get-VBRJobObject (it accepts a job as input). I don't know how your jobs are structured, but for environments I'm in that'd be 10+ objects per job, so I think not great to go in the same report, but you can design that as you like.

Get-VBRInstalledLicense maybe is what you want? Else check the other License related cmdlets. Backup proxy and repo can go into the above report I suppose, as they're part of Job objects.

Give it a whirl and see what you come up with, and ask questions.
David Domask | Product Management: Principal Analyst
g0nzo
Influencer
Posts: 12
Liked: 2 times
Joined: Nov 09, 2019 4:28 pm
Contact:

Re: Help with backup status report to csv

Post by g0nzo »

I Thanks so much for your reply, although I ran you code with took 30 seconds or so and returned nothing. I haven't sent it to cvs yet though.
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Help with backup status report to csv

Post by david.domask »

Hi g0nzo,

Sure, glad to answer.

Run it again and then run $LastDay.

You may be surprised ;)
David Domask | Product Management: Principal Analyst
g0nzo
Influencer
Posts: 12
Liked: 2 times
Joined: Nov 09, 2019 4:28 pm
Contact:

Re: Help with backup status report to csv

Post by g0nzo »

I think I'm being stupid (it is Friday), this is what I'm running:

Code: Select all

using namespace System.Collections.Generic
$jobs = Get-VBRJob
$LastDay =  [List[Object]]@()
foreach($j in $jobs){
     $sess = $j.FindLastSession()
     $objects = Get-VBRJobObject -Job $j
     $JobLastRunDeets = [PSCustomObject]@{
          Name = $j.Name
          Type = $j.JobType
          StartTime = $sess.CreationTime
          Result = $sess.Result
          TargetRepo = $j.GetTargetRepository().Name
          BackupSize = $sess.BackupStats.BackupSize/1024/1024/1024
          }
     $LastDay.Add($JobLastRunDeets)
}
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Help with backup status report to csv

Post by david.domask »

As you said, it's friday :)

The code writes all the data to the List $LastDay

So you run the above, then run $LastDay, like this:

Code: Select all

using namespace System.Collections.Generic
$jobs = Get-VBRJob
$LastDay =  [List[Object]]@()
foreach($j in $jobs){
     $sess = $j.FindLastSession()
     $objects = Get-VBRJobObject -Job $j
     $JobLastRunDeets = [PSCustomObject]@{
          Name = $j.Name
          Type = $j.JobType
          StartTime = $sess.CreationTime
          Result = $sess.Result
          TargetRepo = $j.GetTargetRepository().Name
          BackupSize = $sess.BackupStats.BackupSize/1024/1024/1024
          }
     $LastDay.Add($JobLastRunDeets)
}

$LastDay  #<= this line is what you're missing
That's what you're missing
David Domask | Product Management: Principal Analyst
g0nzo
Influencer
Posts: 12
Liked: 2 times
Joined: Nov 09, 2019 4:28 pm
Contact:

Re: Help with backup status report to csv

Post by g0nzo »

Oh now this is nice! How would I get this to show in a table on the screen and also send it to a CSV?
g0nzo
Influencer
Posts: 12
Liked: 2 times
Joined: Nov 09, 2019 4:28 pm
Contact:

Re: Help with backup status report to csv

Post by g0nzo »

I sorted the csv.

I need to add Objects, Status, Last Run, Next Run, and Description.

I was going to add these myself, but are these the correct names?
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Help with backup status report to csv

Post by david.domask »

Hey g0nzo,

Glad you got it solved.

You can add any property of the job/session objects you desire :)

Save a job as $job = Get-VBRJob -name "some job"

Then do

$job | Get-Member

(you can use gm for short here)

This will show you the methods and properties, and you can just kind of play with it and find what you want, then add it as Some Name = Some Value as shown before.
David Domask | Product Management: Principal Analyst
g0nzo
Influencer
Posts: 12
Liked: 2 times
Joined: Nov 09, 2019 4:28 pm
Contact:

Re: Help with backup status report to csv

Post by g0nzo »

I've managed to get that list off methods and properties.

However I can't seem to get- Status, Last Run, Next Run.

I also want to created a second script off the above to just show "running" jobs too.

Any help would be great!
g0nzo
Influencer
Posts: 12
Liked: 2 times
Joined: Nov 09, 2019 4:28 pm
Contact:

[MERGED] Last day script to show VM backup status in job

Post by g0nzo » 1 person likes this post

Hello,

I have this nice script (thanks David Domask) that shows a summary of our jobs. I've been asked to also list the VMs with in the job and their Name, Status,Start time, End time, Size, Read, Transferred, Duration, Details. I was wondering if someone has done this before?

Code: Select all

using namespace System.Collections.Generic
$jobs = Get-VBRJob
$LastDay =  [List[Object]]@()
foreach($j in $jobs){
     $sess = $j.FindLastSession()
     $objects = Get-VBRJobObject -Job $j
     $JobLastRunDeets = [PSCustomObject]@{
          Name = $j.Name
          Type = $j.JobType
          StartTime = $sess.CreationTime
          Result = $sess.Result
          TargetRepo = $j.GetTargetRepository().Name
          BackupSize = $sess.BackupStats.BackupSize/1024/1024/1024
          }
     $LastDay.Add($JobLastRunDeets)
}

$LastDay
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Last day script to show VM backup status in job

Post by david.domask »

Hey g0nzo,

Please don't split threads. Remember, it's a volunteer forum, and I have a day job too :D

I'll ask the mods to merge this into the other topic.

With these details, you can find them in the information returned from the GetScheduleOptions() method.

Try:

$Jobs = Get-VBRJob
$schedule = $jobs[0].GetScheduleOptions()

And play with $schedule to find the properties you want.

> I've been asked to also list the VMs with in the job and their Name

I showed you this before ;)

Using the same $jobs array above, pass $jobs[0] to Get-VBRJobObject and check the result: https://helpcenter.veeam.com/docs/backu ... ml?ver=110

Keep in mind, if you add via hierarchy containers (e.g., datastores, resource pools, folders, etc), that is what will be returned. Powershell won't do a lookup of the child objects underneath these containers, and you'll need to write some logic to then check with PowerCLI/HyperV's cmdlets to get the VM information.
David Domask | Product Management: Principal Analyst
g0nzo
Influencer
Posts: 12
Liked: 2 times
Joined: Nov 09, 2019 4:28 pm
Contact:

Re: Help with backup status report to csv

Post by g0nzo »

Sorry I only created a new one as I thought this separate request would get missed for others to find/learn as this post is way too long now.

I'll give the above a go. I've manage to get most sent to InfluxDB now and then into our Grafana dashboard.
RubinCompServ
Service Provider
Posts: 261
Liked: 66 times
Joined: Mar 16, 2015 4:00 pm
Full Name: David Rubin
Contact:

Re: Help with backup status report to csv

Post by RubinCompServ »

david.domask wrote: Mar 11, 2022 8:38 pm As you said, it's friday :)

The code writes all the data to the List $LastDay

So you run the above, then run $LastDay, like this:

Code: Select all

using namespace System.Collections.Generic
$jobs = Get-VBRJob
$LastDay =  [List[Object]]@()
foreach($j in $jobs){
     $sess = $j.FindLastSession()
     $objects = Get-VBRJobObject -Job $j
     $JobLastRunDeets = [PSCustomObject]@{
          Name = $j.Name
          Type = $j.JobType
          StartTime = $sess.CreationTime
          Result = $sess.Result
          TargetRepo = $j.GetTargetRepository().Name
          BackupSize = $sess.BackupStats.BackupSize/1024/1024/1024
          }
     $LastDay.Add($JobLastRunDeets)
}

$LastDay  #<= this line is what you're missing
That's what you're missing
Correct me if I'm wrong, but, based on the usage of 'FindLastSession()', it looks like this script doesn't necessarily show job runs for the last day, just the most recent instance of the job run (which could be months ago, for disabled jobs). Correct?
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Help with backup status report to csv

Post by david.domask » 1 person likes this post

Hi @RubinCompServ,

You're correct! I made a bit of an assumption in this code that the jobs are running daily.

If you want to get all runs for a specific day, you'll need to use the Get-VBRBackupSession: https://helpcenter.veeam.com/docs/backu ... ml?ver=110

Sort by CreatimeTime with the -Descending flag, and then you can use Get-Date from Powershell, set it as some variable (e.g., $currDate) and use the AddDays() method on DateTime objects to find all runs for a given date.
David Domask | Product Management: Principal Analyst
RubinCompServ
Service Provider
Posts: 261
Liked: 66 times
Joined: Mar 16, 2015 4:00 pm
Full Name: David Rubin
Contact:

Re: Help with backup status report to csv

Post by RubinCompServ » 1 person likes this post

Thanks @david.domask!

Of course, the overlooked solution here is VeeamONE, but I much prefer PS scripting as a solution :)
david.domask
Veeam Software
Posts: 1226
Liked: 323 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Help with backup status report to csv

Post by david.domask » 2 people like this post

Happy to help! Indeed, VeeamOne handles a lot of this quite fine, but I know that a lot of people prefer to further ingest the report data to other services, and the parsing of the CSV from VeeamOne is not always so easy, so I absolutely agree that PS (and the RestAPI) has its use cases!
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests