Powershell, Success per-VM backup, not job.

PowerShell script exchange

Powershell, Success per-VM backup, not job.

Veeam Logoby Ctek » Fri Aug 05, 2016 2:04 pm

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 9 Certified - Systems Administrator
Ctek
Service Provider
 
Posts: 39
Liked: 3 times
Joined: Wed Nov 11, 2015 3:50 pm
Location: Canada

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

Veeam Logoby tdewin » Fri Aug 05, 2016 2:27 pm

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
tdewin
Veeam Software
 
Posts: 1047
Liked: 361 times
Joined: Fri Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin

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

Veeam Logoby Ctek » Fri Aug 05, 2016 3:05 pm

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 9 Certified - Systems Administrator
Ctek
Service Provider
 
Posts: 39
Liked: 3 times
Joined: Wed Nov 11, 2015 3:50 pm
Location: Canada

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

Veeam Logoby peteraerts » Thu Aug 11, 2016 3:03 pm

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.)
peteraerts
Novice
 
Posts: 5
Liked: never
Joined: Tue Mar 17, 2015 10:15 am
Full Name: Peter Aerts

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

Veeam Logoby tsightler » Fri Aug 12, 2016 1:07 pm

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.
tsightler
Veeam Software
 
Posts: 4772
Liked: 1740 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

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

Veeam Logoby peteraerts » Fri Aug 12, 2016 2:32 pm

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
        }
    }
}
peteraerts
Novice
 
Posts: 5
Liked: never
Joined: Tue Mar 17, 2015 10:15 am
Full Name: Peter Aerts

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

Veeam Logoby tsightler » Sat Aug 13, 2016 6:51 pm 2 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.
tsightler
Veeam Software
 
Posts: 4772
Liked: 1740 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

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

Veeam Logoby peteraerts » Tue Aug 16, 2016 10:02 am

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 !
peteraerts
Novice
 
Posts: 5
Liked: never
Joined: Tue Mar 17, 2015 10:15 am
Full Name: Peter Aerts

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

Veeam Logoby tsightler » Tue Aug 16, 2016 12:59 pm

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.
tsightler
Veeam Software
 
Posts: 4772
Liked: 1740 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 11 guests