PowerShell script exchange
Post Reply
Lpt
Lurker
Posts: 2
Liked: never
Joined: Mar 10, 2024 2:24 pm
Full Name: Laurent Persat
Contact:

Powershell, Success per-Job

Post by Lpt »

Hi,

I've almost no knowledge of Powershell scripting and didn't find existing script to suit my need so i ask here.

I need to extract monthly job results from job but I don't need the per-VM detail found in this old thread : powershell-f26/powershell-success-vm-ba ... 36923.html

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
    }
}
As of today, I've been unsuccessul to find or write a script that fits my needs

I need to get the following properties in the output array : JobName, CreationTime, EndTime, Result (the last of the retry series) , RetryNb (to get to the last result)

Can someone help me ?
oleg.feoktistov
Veeam Software
Posts: 1918
Liked: 636 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell, Success per-Job

Post by oleg.feoktistov »

Hi Laurent,

Here is the sample script how you could obtain the required info without per-VM details:

Code: Select all

$date = (Get-Date).AddMonths(-1)
$sessions = Get-VBRBackupSession | where {$_.CreationTime -ge $date}
foreach ($session in $sessions) {
  $sessionsStack = $session.GetOriginalAndRetrySessions($true)
  $sessionsStack | where {$_.IsRetryMode -eq $false}
  $sessionsStack | select JobName, CreationTime, EndTime, Result
}
Basically, GetOriginalAndRetrySessions() method finds if there are any retries in the job run and then we filter out the last session in the run precisely.

Best regards,
Oleg
Lpt
Lurker
Posts: 2
Liked: never
Joined: Mar 10, 2024 2:24 pm
Full Name: Laurent Persat
Contact:

Re: Powershell, Success per-Job

Post by Lpt »

Hi Oleg,

Yesterday, I have been sucessful to get the the result I wanted. Here's my sample script (seems not very elegant):

Code: Select all

foreach ($job in $jobsList) {
	$BackupSessions = Get-VBRBackupSession | Where {$_.jobId -eq $job.Id.Guid} | Where { ($_.CreationTime -ge $startDate) -and ($_.CreationTime -lt $endDate) } | Sort JobName, CreationTime
	ForEach ($BackupSession in ($BackupSessions | Where { $_.IsRetryMode -eq $false })) {
		$row = "" | Select JobName, StartTime, EndTime, Result, NbRetry

		# Si le résultat de la session est "Failed"
		if ($BackupSession.Result -eq "Failed") {
			# Recherche les sessions de réessai associées
			$RetrySessions = $BackupSessions | Where { ($_.IsRetryMode -eq $true) -and ($_.OriginalSessionId -eq $BackupSession.Id) }
			# Si il y a eu retry sélectionne la dernière session de réessai
			$LastRetrySession = $RetrySessions | Sort CreationTime | Select -Last 1

			$row.JobName = $LastRetrySession.JobName
			$row.StartTime = $LastRetrySession.Info.CreationTime
			$row.EndTime = $LastRetrySession.Info.EndTime
			$row.Result = $LastRetrySession.Info.Result
			$row.NbRetry = $RetrySessions.Count
		} else {
			$row.JobName = $BackupSession.JobName
			$row.StartTime = $BackupSession.Info.CreationTime
			$row.EndTime = $BackupSession.Info.EndTime
			$row.Result = $BackupSession.Info.Result
			$row.NbRetry = 0
		}
		
		$Report += $row
	}

}
I will try the method GetOriginalAndRetrySessions() if I'm not too lazy ... not the case at the moment

Thanks

Best regards,
Laurent
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests