PowerShell script exchange
Post Reply
Ctek
Service Provider
Posts: 84
Liked: 13 times
Joined: Nov 11, 2015 3:50 pm
Location: Canada
Contact:

Powershell, Success per-VM backup, not job.

Post by Ctek »

Hi,

I am looking to sharpen my script that I have here. Since we have over 1000VM spread into our Veeam servers, I would like to get the success/failure status PER VM instead of per job. How would I get that info?

Thank you.
VMCE
tdewin
Veeam Software
Posts: 1818
Liked: 655 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by tdewin » 1 person likes this post

You could use Veeam ONE which has a preconfigured report ;)

But you can also look up the backup sessions, and than look for the individual task sessions. The first line is just to get a single job in $j .

Code: Select all

$j = @(get-vbrjob)[0]
$s = $j.FindLastSession()
$s.GetTaskSessions() | select Name,Status
Gives you something like this

Code: Select all

Name                                                                     Status
----                                                                     ------
exchange2013wit1                                                        Success
exchange2013mbx2                                                        Success
exchange2013mbx1                                                        Success
demo-win2                                                               Success
demo-win1                                                               Success
demo-SQLandSP                                                           Success
demo-sql-ao-db2                                                         Success
demo-sql-ao-db1                                                         Success
demo-Oracle                                                             Success
demo-linux1                                                             Success
demo-linux2                                                             Success
demo-Exchange                                                           Success
demo-domino                                                             Success
demo-AD                                                                 Success
*Edit* an even more supported way would be to get the task sessions via the correct cmdlet (get-vbrtasksession)
https://helpcenter.veeam.com/backup/pow ... ssion.html
Ctek
Service Provider
Posts: 84
Liked: 13 times
Joined: Nov 11, 2015 3:50 pm
Location: Canada
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by Ctek »

Alright, I think I got what I want :

Code: Select all

Get-VBRBackupSession | ? {$_.JobType -eq "backup"} | Get-VBRTaskSession | select name,status
Returns VM and Status.

Thanks!
VMCE
peteraerts
Novice
Posts: 5
Liked: never
Joined: Mar 17, 2015 10:15 am
Full Name: Peter Aerts
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by peteraerts »

I also need such a list of status per VM, but for a whole month and also with dates.
I created such a script, using Get-VBRBackupSession and Get-VBRTaskSession, which works fine, except when there are retries. For instance if I have a job containing 10 VMs and 1 fails, then succeeds after 2 retries, my output file will contain 12 lines for that job. And I would like to have only 10, with the status of each VM containing the result of the last retry (if any).

Currently I open the output file in Excel and manually find and change the status of a failed VM if I find a successful retry, then delete the retry line to end up with the right number of lines per job. But this takes quite some time, so I would like to have these retries consolidated already in my output file.
Any ideas or tips?

(BTW we do have Veeam One available, but I need to use a custom template for the report, which calculates the percentage of successful VMs per job per day.)
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by tsightler »

Could you share the code you have now? That would probably make it easier to offer a suggestion on how to deal with retries for your case. In summary, you need to code up exactly what you are doing now, effectively, gather the main backup session and any retry sessions, grab the task sessions for all of that, then eliminate any failed tasks sessions that were later successful on retry.
peteraerts
Novice
Posts: 5
Liked: never
Joined: Mar 17, 2015 10:15 am
Full Name: Peter Aerts
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by peteraerts »

I left out some prompts for $StartDate and $EndDate (eg first and last day of the previous month). When I have those dates I run the below commands to build the $Result list.
In the script I get job name, session name, session result, start and end datetime, a short date (for easy pivot table by date in Excel) and the status per VM.
My main problem is with, as you said, "eliminating any failed retries that were later successful on retry".

Code: Select all

$BackupSessions = Get-VBRBackupSession | where {(($_.CreationTime -ge $StartDate) -and ($_.CreationTime -le $EndDate))} | Sort JobName, CreationTime
$Result = & { ForEach ($BackupSession in $BackupSessions) {
    ForEach ($TaskSession in ($BackupSession | Get-VBRTaskSession)) {
        $TaskSession | Select @{N="JobName";E={$BackupSession.JobName}},@{N="SessionName";E={$BackupSession.Name}},@{N="JobResult";E={$BackupSession.Result}},@{N="JobStart";E={$BackupSession.CreationTime}},@{N="JobEnd";E={$BackupSession.EndTime}},@{N="Date";E={$BackupSession.CreationTime.ToString("yyyy-MM-dd")}},name,status
        }
    }
}
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by tsightler » 3 people like this post

OK, so here's what I came up with:

Code: Select all

$BackupSessions = Get-VBRBackupSession | where {(($_.CreationTime -ge $StartDate) -and ($_.CreationTime -le $EndDate))} | Sort JobName, CreationTime
$Result = & {
    ForEach ($BackupSession in ($BackupSessions | ?{$_.IsRetryMode -eq $false})) {
        [System.Collections.ArrayList]$TaskSessions = @($BackupSession | Get-VBRTaskSession)
        If ($BackupSession.Result -eq "Failed") {
            $RetrySessions = $BackupSessions | ?{($_.IsRetryMode -eq $true) -and ($_.OriginalSessionId -eq $BackupSession.Id)}
            ForEach ($RetrySession in $RetrySessions) {
                [System.Collections.ArrayList]$RetryTaskSessions = @($RetrySession | Get-VBRTaskSession)
                ForEach ($RetryTaskSession in $RetryTaskSessions) {
                    $PriorTaskSession = $TaskSessions | ?{$_.Name -eq $RetryTaskSession.Name}
                    If ($PriorTaskSession) { $TaskSessions.Remove($PriorTaskSession) }
                    $TaskSessions.Add($RetryTaskSession) | Out-Null   
                }
            }
        }
    $TaskSessions | Select @{N="JobName";E={$BackupSession.JobName}},@{N="SessionName";E={$_.JobSess.Name}},@{N="JobResult";E={$_.JobSess.Result}},@{N="JobStart";E={$_.JobSess.CreationTime}},@{N="JobEnd";E={$_.JobSess.EndTime}},@{N="Date";E={$_.JobSess.CreationTime.ToString("yyyy-MM-dd")}},name,status
    }
}
This code starts by loading the task sessions into an array list so that they can be easily modified. If a job ends in Success or Warning, there's effectively no difference in behavior from the previous code, however, if the job ended in Failure status it starts a loop that grabs all retry sessions for the job, loops through all the task sessions in those retries, and replaces older tasks sessions in the array list with the task session results from the retry sessions.

I also slightly modified the output to display the retry sessions in the SessionName field, which gives a visual indication of the retries, and also uses the retry session start and end time instead of the times from the master job session, otherwise I felt like the display was not really very accurate. Only a single, final result is displayed for each VM for a given master job and it's subsequent retries.

One possible gotcha is if there are any duplicate VM names within the same job. Right now I'm matching tasks sessions based on VM name because I couldn't figure out a more reliable way to do it. I tried using the ObjectId, which I thought would be more reliable, but unfortunately certain failure modes cause this field not to be populated (for example if a hosts is offline, etc). I couldn't determine any other more reliable way with the available data in the task session, so I just went with VM name for now.

I've tested this over the last 12 months of data that I have in my VBR environment and it seemed to report accurately but there could be bugs lurking so feel free to let me know.
peteraerts
Novice
Posts: 5
Liked: never
Joined: Mar 17, 2015 10:15 am
Full Name: Peter Aerts
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by peteraerts »

This seems to work very well, thanks! I'm not so good at array stuff, and didn't know about IsRetryMode, gotta dig deeper into that.

We don't have duplicate VM names in one job, so no possible issue there. The only minor problem I had was for a job which had failed even after retries, where I had fixed the issue then started the job again manually. This is not regarded as the same session, so I had 2 results for that one day. But not your script's fault of course.

Thanks again, great product and great support !
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by tsightler »

peteraerts wrote:The only minor problem I had was for a job which had failed even after retries, where I had fixed the issue then started the job again manually. This is not regarded as the same session, so I had 2 results for that one day. But not your script's fault of course.
Yes, I thought about this as well because I had some similar sessions in my environment, but the only thing I could think of was to make the assumption that a manually run session should be considered part of the prior session, and that didn't seem like a safe way to go. However, rather than manually starting the job in such a case, you can manually retry the job, and those retries will still be considered part of the original session. Glad the code seems to be working for you.
pra_seano
Lurker
Posts: 1
Liked: never
Joined: Mar 29, 2022 8:39 pm
Full Name: Sean OBrien
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by pra_seano »

I know this is a long shot reviving this post from years ago but the code above from tsightler is near perfect for what I am trying to do and was able to adjust it a bit to my requirements but there is a piece missing that I cant seem to figure out - how to get this to include Agent based backups. It appears the Get-VBRBackupSession command doesnt include these and the Get-VBRComputerBackupJobSession doesnt have nearly the amount of detail available. How can I get both VMware type and Agent type backups in a single report?
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell, Success per-VM backup, not job.

Post by oleg.feoktistov »

Hi Sean,

What exact info on agent backup sessions do you need that is missing in Get-VBRComputerBackupJobSession output?

Thanks,
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests