PowerShell script exchange
Post Reply
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

List only last Full Backup

Post by dellock6 »

Hi all,
bacause of my poor powrshell skills, I'm stuck at creating a simple script. The code right now is:

Code: Select all

Get-VBRBackupSession | where {S._JobType -eq "Backup" -AND S._IsFullMode -eq "True" -AND S._IsCompleted -eq "True" -AND S._Result -ne "Failed"  | select JobName, EndTime | sort JobName, EndTime 
The code is working as expected, the only problem is a job with multiple full backups ended successfully, return me multiple lines in the final table. How can I enhance the script to show me only the newer backup of each job?

Thanks.
Luca.
Luca Dell'Oca
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List only last Full Backup

Post by veremin » 1 person likes this post

In the way I see it, something like this should work fine:

Code: Select all

asnp VeeamPSSnapin
foreach($job in (Get-VBRJob | ? {$_.JobType -eq "Backup"}))
{
Get-VBRBackupSession | ?{$_.Jobname -eq $Job.name -and $_.JobType -eq "Backup" -AND $_.IsFullMode -eq "True" -eq "True" -AND $_.IsCompleted -eq "True" -AND $_.Result -ne "Failed"} | Sort-Object EndTime -Descending | Select-Object -First 1 | select JobName, EndTime
} 
Hope this helps.
Thanks.
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

Re: List only last Full Backup

Post by dellock6 »

Works beautifully, thanks Vladimir (also for the code snippet to be re-used in other situations).

Luca.
Luca Dell'Oca
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

Re: List only last Full Backup

Post by dellock6 »

Just an add: this script only shows completed full backups at first pass, if for some reason the full is completed after a retry for example (even for only a subset of the VMs included in the job) the job itself would not be listed by the script.

Luca.
Luca Dell'Oca
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List only last Full Backup

Post by veremin »

What about using additional parameter for retried sessions:

Code: Select all

$_.isretrymode -eq $true 
Hope this helps.
Thanks.
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

Re: List only last Full Backup

Post by dellock6 »

Thinking about it right now :)
I tried to add this in the original script in different positions as a -OR condition, but it return me the same small list, or the last retry execution regardless is a full or a reverse incremental... Not sure where and how to best place this piece of code...

Luca.
Luca Dell'Oca
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List only last Full Backup

Post by veremin »

Unfortunately, there are no retried jobs with successful full backup in my lab, thus, it’s a little hard to test.

However, you can try to implement something like this and see whether it works. This script should list all successful full backup sessions either the retry mode is $true or $false:

Code: Select all

asnp VeeamPSSnapin
foreach($job in (Get-VBRJob | ? {$_.JobType -eq "Backup"}))
{
Get-VBRBackupSession | ?{$_.Jobname -eq $Job.name -and $_.JobType -eq "Backup" -and $_.IsFullMode -eq "True" -eq "True" -and $_.IsCompleted -eq "True" -AND $_.Result -ne "Failed" -and ($_.isretrymode -eq $False -or $True)} | Sort-Object EndTime -Descending | Select-Object -First 1 | select JobName, EndTime
}  
Hope this helps.
Thanks.
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

Re: List only last Full Backup

Post by dellock6 »

Uhm, seems the same list is coming out as in the previous version of the script.
Anyway, at this stage the script is ok, it's also a way to know if for some reason a backup job has NEVER been able to complete without error.

Luca.
Luca Dell'Oca
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List only last Full Backup

Post by veremin »

I’ve already changed slightly script provided above, and may be the following example will work as supposed:

Code: Select all

asnp VeeamPSSnapin
foreach($job in (Get-VBRJob | ? {$_.JobType -eq "Backup"}))
{
Get-VBRBackupSession | ?{$_.Jobname -eq $Job.name -and $_.JobType -eq "Backup" -and $_.IsFullMode -eq "True" -eq "True" -and $_.IsCompleted -eq "True" -and $_.Result -ne "Failed" -and ($_.isretrymode -eq $False -or $_.isretrymode -eq $True)} | Sort-Object EndTime -Descending | Select-Object -First 1 | select JobName, EndTime
}   
If it doesn’t, I will ask you to confirm that there are indeed retried full backup sessions which have finished successfully in your environment.
It can be done by running modified version of the script:

Code: Select all

asnp VeeamPSSnapin
foreach($job in (Get-VBRJob | ? {$_.JobType -eq "Backup"}))
{
Get-VBRBackupSession | ?{$_.Jobname -eq $Job.name -and $_.JobType -eq "Backup" -and $_.IsFullMode -eq "True" -eq "True" -and $_.IsCompleted -eq "True" -AND $_.Result -ne "Failed" -and $_.isretrymode -eq $True)} | Sort-Object EndTime -Descending | Select-Object -First 1 | select JobName, EndTime
}
Hope this helps.
Thanks.
dellock6
Veeam Software
Posts: 6137
Liked: 1928 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

Re: List only last Full Backup

Post by dellock6 »

Ah, the second script returns no result, so it seems there are no retried full backups. Maybe this is the reason why changing the original script gives no difference in the results....
So I may say the last version of the script is good to be used...

Luca.
Luca Dell'Oca
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List only last Full Backup

Post by veremin »

So, I may say the last version of the script is good to be used.
Yep, I believe so.

Should any other questions regarding it arise, don’t hesitate to let me know.

Hope this helps.
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests