PowerShell script exchange
Post Reply
waku2022
Lurker
Posts: 1
Liked: never
Joined: Sep 07, 2022 6:22 am
Full Name: Jordan Bailey
Contact:

Last backup status

Post by waku2022 »

Hi,

Im looks for some help with creating a powershell script to create a text document with the name of the Veeam server and last backup status of all warning and failed jobs.

i've been playing with get-vbrjob -name * | select-object 'last result'

but there doesn't seem to be a last result property that i can select. im pretty new to powershell so this is all very daunting for me. but it would definetly help my day if i could get an output of last result like it shows when you run get-vbrjob

i also want to exclude succeeded and disabled jobs.

Thanks for reading.
oleg.feoktistov
Veeam Software
Posts: 1918
Liked: 636 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Last backup status

Post by oleg.feoktistov »

Hi Jordan,

There are two approaches to getting last results for a job. One is through .NET methods, which is fast, but unsupported. For example:

Code: Select all

$jobs = Get-VBRJob
$filteredJobs = @()
foreach ($job in $jobs) {
  $lastResult = $job.GetLastResult()
  $isScheduleEnabled = $job.IsScheduleEnabled
  if ($lastResult -eq 'Failed' -or $lastResult -eq 'Warning' -and $isScheduleEnabled -eq $true) {
    $filteredJobs += $job
  }
}
The other one is through official cmdlets, which may be slower, but is supported:

Code: Select all

$jobs = Get-VBRJob
$filteredJobs = @()
foreach ($job in $jobs) {
    if ($job.IsScheduleEnabled -eq $true) {
    $sessions = Get-VBRBackupSession -Name "$($job.Name)*" | where {$_.Result -eq 'Warning' -or $_.Result -eq 'Failed'}
    $lastSession = $sessions | sort -Property CreationTimeUTC | select -Last 1
    if ($lastSession) {
    $jobsFiltered += $job | select Name, @{n='LastResult';e={$lastSession.Result}}
    }
    }
}

$agentJobs = Get-VBRComputerBackupJob
$sessions = Get-VBRComputerBackupJobSession
foreach ($agentJob in $agentJobs) {
  if ($agentJob.JobEnabled -eq $true) {
  $jobSessions = $sessions | where {$_.JobId -eq $agentJob.Id} 
  $filteredSessions = $jobSessions | where {$_.Result -eq 'Failed' -or $_.Result -eq 'Warning'}
  if ($filteredSessions) {
  $lastSession = $filteredSessions | sort -Property CreationTime | select -Last 1
  $filteredJobs += $agentJob | select Name, @{n='LastResult';e={$lastSession.Result}}
  }
  }
}
Please note that in the second example I used a separate cmdlet for agent management jobs. That's because although Get-VBRJob cmdlet reflects some info on such jobs, it doesn't support them. With the second approach you would also need to use separate cmdlets, for instance, for agent management copy jobs - Get-VBRComputerBackupCopyJob, nas backup jobs - Get-VBRNASBackupJob etc.

Best regards,
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests