PowerShell script exchange
Post Reply
feck
Novice
Posts: 3
Liked: never
Joined: Sep 02, 2021 7:04 am
Full Name: work@lusinski.co.uk
Contact:

"for the last 4 days, list failures which don't have a more recent success" by PowerShell or cmd or SQL, please?

Post by feck »

Could anyone shed any light on how I might:
"for the last 4 days, list failures which don't have a more recent success" by PowerShell or cmd or SQL, or some scripting method, please?

Case # 04998824 [ ref:_00D30RWR._5007V1ryV4I:ref ]

Thanks
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: "for the last 4 days, list failures which don't have a more recent success" by PowerShell or cmd or SQL, please?

Post by oleg.feoktistov »

Hi Stefan,

I reviewed the case you shared. Seems like there is a bit of misunderstanding going on with Get-VBRJob cmdlet, so let me clarify. This cmdlet is no longer supported for agent backup jobs (where agent is referenced as computer in a warning). For other common platforms like Vmware, Hyper-V it is still supported, so you can normally use it. Beside that, since you are interested in individual job runs with particular statuses, you are after backup sessions, not job entities. So, this snippet should work fine for your case:

Code: Select all

$jobs = Get-VBRJob
$date = (Get-Date).AddDays(-4)
$sessionsTotal = @()
foreach ($job in $jobs) {
$sessionsForJob = Get-VBRBackupSession -Name "$($job.Name)*" | where {$_.EndTime -ge $date -and $_.Result -eq 'Failed'}
$sessionsTotal += $sessionsForJob
}
$sessionsTotal | select Name, EndTime, Result 


Let me know if it helped,
Oleg
feck
Novice
Posts: 3
Liked: never
Joined: Sep 02, 2021 7:04 am
Full Name: work@lusinski.co.uk
Contact:

Re: "for the last 4 days, list failures which don't have a more recent success" by PowerShell or cmd or SQL, please?

Post by feck »

Oleg
Thank-you for your response. This is definitely along the right lines.

Would you kindly advise on how to address the "...which don't have a more recent success" part of the requirement, please.

Consider a scenario where a backup/session/whatever has failed. The script advises which backup has failed, so I re-run the failed item and if it succeeds, when I next run the script I should see no rows returned.

Thanks
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: "for the last 4 days, list failures which don't have a more recent success" by PowerShell or cmd or SQL, please?

Post by oleg.feoktistov »

Stefan,

Sure, just add an if statement to filter out last success states for each job:

Code: Select all

$jobs = Get-VBRJob
$date = (Get-Date).AddDays(-4)
$sessionsTotal = @()

foreach ($job in $jobs) {
	$lastResult = $Job.GetLastResult()
	if ($lastResult -ne 'Success') { # Newly added if statement 
	$sessionsForJob = Get-VBRBackupSession -Name "$($job.Name)*" | where {$_.EndTime -ge $date -and $_.Result -eq 'Failed'}
	$sessionsTotal += $sessionsForJob
    }
}
$sessionsTotal | select Name, EndTime, Result 
Thanks,
Oleg
feck
Novice
Posts: 3
Liked: never
Joined: Sep 02, 2021 7:04 am
Full Name: work@lusinski.co.uk
Contact:

Re: "for the last 4 days, list failures which don't have a more recent success" by PowerShell or cmd or SQL, please?

Post by feck »

Oleg

Tremendous! Thank-you very much for your prompt and perfect response.

How ought I proceed if I have a finite set jobs, say 5, which the script ought report on?

One way would be to put a look around the existing code for a pre-populated array, but that would mean invoking the Get-VBRJob cmdlet 5 times, so possibly inefficient?

Is there some construct like the below?

Code: Select all

$jobs = Get-VBRJob | where name in ('job1', 'job2', 'job3');
Finally, it would be great to pass the list of jobs to the script as parameters.

Thanks again.
Stefan
soncscy
Veteran
Posts: 643
Liked: 312 times
Joined: Aug 04, 2019 2:57 pm
Full Name: Harvey
Contact:

Re: "for the last 4 days, list failures which don't have a more recent success" by PowerShell or cmd or SQL, please?

Post by soncscy »

Hey Stefan,

Sure, you can do operators against an array, for example, Get-VBRJob | Where-Object{$names -contains $_.name}. You can also string together conditions in the Where-Object{} expression with -or operator.

This would require you pre-populate the $names array of course, so either hard-code it or use some logic to pull desired jobs that way. I would actually do it on $_.JobID instead of name just for convenience and avoiding parsing errors (parse don't validate when it comes to strings!)

But as I see it you're used to TSQL syntax there, so the equivalent of where @Value in ('set','of','items') would be with the Where-Object function.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 9 guests