PowerShell script exchange
Post Reply
Dexiner
Novice
Posts: 6
Liked: never
Joined: Oct 04, 2019 1:15 pm
Full Name: AAAA
Contact:

Report as failed only after 3 retries.

Post by Dexiner »

Hi.
I want my script to display as failed only after 3 retries because the Job sometimes fails at first try but succeed at second/third.
I'm using the standard retry option from Veeam.
Is there a way to do this?
Here is the code

Code: Select all


$pshost = get-host
$pswindow = $pshost.ui.rawui

$newsize = $pswindow.buffersize
$newsize.height = 300
$newsize.width = 150
$pswindow.buffersize = $newsize

# Get Information from veeam backup and replication in cmk-friendly format
# V0.9
# Load Veeam Backup and Replication Powershell Snapin
Add-PSSnapin VeeamPSSnapIn -ErrorAction SilentlyContinue

try
{
$tapeJobs = Get-VBRTapeJob
write-host "<<<veeam_tapejobs>>>"
write-host "JobName JobID LastResult LastState"
foreach ($tapeJob in $tapeJobs)
    {
        $jobName = $tapeJob.Name
        $jobID = $tapeJob.Id
        $lastResult = $tapeJob.LastResult
        $lastState = $tapeJob.LastState
        write-host "$jobName $jobID $lastResult $lastState"
    }


$myJobsText = "<<<veeam_jobs:sep(9)>>>`n"
$myTaskText = ""

$myBackupJobs = Get-VBRJob | where {$_.IsScheduleEnabled -eq $true }

foreach ($myJob in $myBackupJobs)
    {
	$myJobName = $myJob.Name -replace "\'","_" -replace " ","_"

	$myJobType = $myjob.JobType

	$myJobLastState = $myJob.GetLastState()

	$myJobLastResult = $myJob.GetLastResult()

	$myJobLastSession = $myJob.FindLastSession()

	$myJobCreationTime = $myJobLastSession.CreationTime |  get-date -Format "dd.MM.yyyy HH\:mm\:ss"  -ErrorAction SilentlyContinue

	$myJobEndTime = $myJobLastSession.EndTime |  get-date -Format "dd.MM.yyyy HH\:mm\:ss"  -ErrorAction SilentlyContinue

	$myJobsText = "$myJobsText" + "$myJobName" + "`t" + "$myJobType" + "`t" + "$myJobLastState" + "`t" + "$myJobLastResult" + "`t" + "$myJobCreationTime" + "`t" + "$myJobEndTime" + "`n"

	# For Non Backup Jobs (Replicas) we bail out
	# because we are interested in the status of the original backup but
	# for replicas the overall job state is all we need.
        if ($myJob.IsBackup -eq $false) { continue }

	# Each backup job has a number of tasks which were executed (VMs which were processed)
	# Get all Tasks of the  L A S T  backup session
	# Caution: Each backup job MAY have run SEVERAL times for retries,
	# thats why we need all sessions related to the last one if its a retry
	$sessions = @($myJobLastSession)
	if ($myJobLastSession.IsRetryMode)
	{
	$sessions = $myJobLastSession.GetOriginalAndRetrySessions($TRUE)
	}

	$myJobLastSessionTasks = $sessions | Get-VBRTaskSession  -ErrorAction SilentlyContinue

	foreach ($myTask in $myJobLastSessionTasks)
	{
		$myTaskName = $myTask.Name

		$myTaskText = "$myTaskText" + "<<<<" + "$myTaskName" + ">>>>" + "`n"

		$myTaskText = "$myTaskText" + "<<<"+ "veeam_client:sep(9)" +">>>" +"`n"

		$myTaskStatus = $myTask.Status

		$myTaskText = "$myTaskText" + "Status" + "`t" + "$myTaskStatus" + "`n"

		$myTaskText = "$myTaskText" + "JobName" + "`t" + "$myJobName" + "`n"

		$myTaskTotalSize = $myTask.Progress.TotalSize

		$myTaskText = "$myTaskText" + "TotalSizeByte" + "`t" + "$myTaskTotalSize" + "`n"

		$myTaskReadSize = $myTask.Progress.ReadSize

		$myTaskText = "$myTaskText" + "ReadSizeByte" + "`t" + "$myTaskReadSize" + "`n"

		$myTaskTransferedSize = $myTask.Progress.TransferedSize

		$myTaskText = "$myTaskText" + "TransferedSizeByte" + "`t" + "$myTaskTransferedSize" + "`n"

    # Starting from Version 9.5U3 StartTime is not supported anymore
    If ($myTask.Progress.StartTime -eq $Null) {
		   $myTaskStartTime = $myTask.Progress.StartTimeLocal
    } Else {
		   $myTaskStartTime = $myTask.Progress.StartTime
    }
    $myTaskStartTime = $myTaskStartTime | Get-Date -Format "dd.MM.yyyy HH\:mm\:ss" -ErrorAction SilentlyContinue

		$myTaskText = "$myTaskText" + "StartTime" + "`t" + "$myTaskStartTime" + "`n"

    # Starting from Version 9.5U3 StopTime is not supported anymore
    If ($myTask.Progress.StopTime -eq $Null) {
    		$myTaskStopTime = $myTask.Progress.StopTimeLocal
    } Else {
    		$myTaskStopTime = $myTask.Progress.StopTime
    }
    $myTaskStopTime = $myTaskStopTime | Get-Date -Format "dd.MM.yyyy HH\:mm\:ss" -ErrorAction SilentlyContinue

		$myTaskText = "$myTaskText" + "StopTime" + "`t" + "$myTaskStopTime" + "`n"

		# Result is a value of type System.TimeStamp. I'm sure there is a more elegant way of formatting the output:
		$myTaskDuration = "" + "{0:D2}" -f $myTask.Progress.duration.Days + ":" + "{0:D2}" -f $myTask.Progress.duration.Hours + ":" + "{0:D2}" -f $myTask.Progress.duration.Minutes + ":" + "{0:D2}" -f $myTask.Progress.duration.Seconds

		$myTaskText = "$myTaskText" + "DurationDDHHMMSS" + "`t" + "$myTaskDuration" + "`n"

		$myTaskAvgSpeed = $myTask.Progress.AvgSpeed

		$myTaskText = "$myTaskText" + "AvgSpeedBps" + "`t" + "$myTaskAvgSpeed" + "`n"

		$myTaskDisplayName = $myTask.Progress.DisplayName

		$myTaskText = "$myTaskText" + "DisplayName" + "`t" + "$myTaskDisplayName" + "`n"

		$myBackupHost = Hostname

		$myTaskText = "$myTaskText" + "BackupServer" + "`t" + "$myBackupHost" + "`n"

		$myTaskText = "$myTaskText" + "<<<<" + ">>>>" +"`n" +"`n" +"`n"

	}

    }

write-host $myJobsText
write-host $myTaskText
}

catch
{
$errMsg = $_.Exception.Message
$errItem = $_.Exception.ItemName
Write-Error "Totally unexpected and unhandled error occured:`n Item: $errItem`n Error Message: $errMsg"
Break
}

jhoughes
Expert
Posts: 279
Liked: 112 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Castle Rock, CO
Contact:

Re: Report as failed only after 3 retries.

Post by jhoughes »

I'm not at a console to code it currently, but you should only need to sort your $sessions variable by their start times, then just select the first object to get that specific task session.
Husband, Father, Solutions Architect, Geek Extraordinaire | @DenverVMUG, @AustinVMUG & @ATXPowerShell leader | VMware vExpert | Cisco Champion

Dexiner
Novice
Posts: 6
Liked: never
Joined: Oct 04, 2019 1:15 pm
Full Name: AAAA
Contact:

Re: Report as failed only after 3 retries.

Post by Dexiner »

I don't think I understood what you meant

nielsengelen
Veeam Software
Posts: 4572
Liked: 943 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Report as failed only after 3 retries.

Post by nielsengelen »

What Joe is saying is that if u go over the $sessions variabele, you can see all 3 runs per job (if it happens). If you then select the last run, you will have the desired output.
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen

Dexiner
Novice
Posts: 6
Liked: never
Joined: Oct 04, 2019 1:15 pm
Full Name: AAAA
Contact:

Re: Report as failed only after 3 retries.

Post by Dexiner »

My $session contains all the backups, that's not going to work

veremin
Product Manager
Posts: 18857
Liked: 1905 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Report as failed only after 3 retries.

Post by veremin » 1 person likes this post

I think you can simply get the latest job session, check, whether it will be retried, and if not query its status. Something like this:

Code: Select all

asnp VeeamPSSnapin
$Job = Get-VBRJob -name "Name of your Backup job"
$Session = $Job.FindLastSession()
$Session.willberetried
Thanks!

Zach123
Enthusiast
Posts: 40
Liked: 2 times
Joined: Jun 04, 2019 12:36 am
Full Name: zaki khan
Contact:

Re: Report as failed only after 3 retries.

Post by Zach123 »

Hi Veremin

How can we do that in v11 for agent jobs? Below are the only things listed when I say get-member.
Want to see the status of only the last retry attempt for each backup job

Code: Select all

Name           MemberType            Definition
----           ----------            ----------
Add            Method                void Add(Veeam.Backup.PowerShell.Infos.VBRSession item), void ICollection[VBRSession].Add(Veeam.Backup.PowerShell.Inf...
AddRange       Method                void AddRange(System.Collections.Generic.IEnumerable[Veeam.Backup.PowerShell.Infos.VBRSession] collection)
AsReadOnly     Method                System.Collections.ObjectModel.ReadOnlyCollection[Veeam.Backup.PowerShell.Infos.VBRSession] AsReadOnly()
BinarySearch   Method                int BinarySearch(int index, int count, Veeam.Backup.PowerShell.Infos.VBRSession item, System.Collections.Generic.ICom...
Clear          Method                void Clear(), void ICollection[VBRSession].Clear(), void IList.Clear()
Contains       Method                bool Contains(Veeam.Backup.PowerShell.Infos.VBRSession item), bool ICollection[VBRSession].Contains(Veeam.Backup.Powe...
ConvertAll     Method                System.Collections.Generic.List[TOutput] ConvertAll[TOutput](System.Converter[Veeam.Backup.PowerShell.Infos.VBRSessio...
CopyTo         Method                void CopyTo(Veeam.Backup.PowerShell.Infos.VBRSession[] array), void CopyTo(int index, Veeam.Backup.PowerShell.Infos.V...
Equals         Method                bool Equals(System.Object obj)
Exists         Method                bool Exists(System.Predicate[Veeam.Backup.PowerShell.Infos.VBRSession] match)
Find           Method                Veeam.Backup.PowerShell.Infos.VBRSession Find(System.Predicate[Veeam.Backup.PowerShell.Infos.VBRSession] match)
FindAll        Method                System.Collections.Generic.List[Veeam.Backup.PowerShell.Infos.VBRSession] FindAll(System.Predicate[Veeam.Backup.Power...
FindIndex      Method                int FindIndex(System.Predicate[Veeam.Backup.PowerShell.Infos.VBRSession] match), int FindIndex(int startIndex, System...
FindLast       Method                Veeam.Backup.PowerShell.Infos.VBRSession FindLast(System.Predicate[Veeam.Backup.PowerShell.Infos.VBRSession] match)
FindLastIndex  Method                int FindLastIndex(System.Predicate[Veeam.Backup.PowerShell.Infos.VBRSession] match), int FindLastIndex(int startIndex...
ForEach        Method                void ForEach(System.Action[Veeam.Backup.PowerShell.Infos.VBRSession] action)
GetEnumerator  Method                System.Collections.Generic.List`1+Enumerator[Veeam.Backup.PowerShell.Infos.VBRSession] GetEnumerator(), System.Collec...
GetHashCode    Method                int GetHashCode()
GetRange       Method                System.Collections.Generic.List[Veeam.Backup.PowerShell.Infos.VBRSession] GetRange(int index, int count)
GetType        Method                type GetType()
IndexOf        Method                int IndexOf(Veeam.Backup.PowerShell.Infos.VBRSession item), int IndexOf(Veeam.Backup.PowerShell.Infos.VBRSession item...
Insert         Method                void Insert(int index, Veeam.Backup.PowerShell.Infos.VBRSession item), void IList[VBRSession].Insert(int index, Veeam...
InsertRange    Method                void InsertRange(int index, System.Collections.Generic.IEnumerable[Veeam.Backup.PowerShell.Infos.VBRSession] collection)
LastIndexOf    Method                int LastIndexOf(Veeam.Backup.PowerShell.Infos.VBRSession item), int LastIndexOf(Veeam.Backup.PowerShell.Infos.VBRSess...
Remove         Method                bool Remove(Veeam.Backup.PowerShell.Infos.VBRSession item), bool ICollection[VBRSession].Remove(Veeam.Backup.PowerShe...
RemoveAll      Method                int RemoveAll(System.Predicate[Veeam.Backup.PowerShell.Infos.VBRSession] match)
RemoveAt       Method                void RemoveAt(int index), void IList[VBRSession].RemoveAt(int index), void IList.RemoveAt(int index)
RemoveRange    Method                void RemoveRange(int index, int count)
Reverse        Method                void Reverse(), void Reverse(int index, int count)
Sort           Method                void Sort(), void Sort(System.Collections.Generic.IComparer[Veeam.Backup.PowerShell.Infos.VBRSession] comparer), void...
ToArray        Method                Veeam.Backup.PowerShell.Infos.VBRSession[] ToArray()
ToString       Method                string ToString()
TrimExcess     Method                void TrimExcess()
TrueForAll     Method                bool TrueForAll(System.Predicate[Veeam.Backup.PowerShell.Infos.VBRSession] match)
Item           ParameterizedProperty Veeam.Backup.PowerShell.Infos.VBRSession Item(int index) {get;set;}
Capacity       Property              int Capacity {get;set;}
Count          Property              int Count {get;}
IsFixedSize    Property              bool IsFixedSize {get;}
IsReadOnly     Property              bool IsReadOnly {get;}
IsSynchronized Property              bool IsSynchronized {get;}
SyncRoot       Property              System.Object SyncRoot {get;}

Vitaliy S.
Product Manager
Posts: 25284
Liked: 2219 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: Report as failed only after 3 retries.

Post by Vitaliy S. »

Hi Zaki,

For agent jobs, you need to use this cmdlet > Get-VBRComputerBackupJob

Thanks!

Zach123
Enthusiast
Posts: 40
Liked: 2 times
Joined: Jun 04, 2019 12:36 am
Full Name: zaki khan
Contact:

Re: Report as failed only after 3 retries.

Post by Zach123 »

Hi Vitaliy

Thanks for the reply. I am still not clear on that.

For eg, If I use below command , it shows the VM backup session that failed even after third retry.
Get-VBRBackupSession | Where-Object { ($_.Result -eq "Failed") -and ($_.WillBeRetried -ne "True") }

But If I use the same command and replace Get-VBRBackupSession with Get-VBRComputerBackupJobSession to list the agent sessions which are failed even after third attempt , it does not work as the command does not have "WillBeRetried" as an available method to use.
Get-VBRComputerBackupJobSession | Where-Object { ($_.Result -eq "Failed") -and ($_.WillBeRetried -ne "True") }

I am not sure how can I get this info from "Get-VBRComputerBackupJob" as suggested as I am looking for the agent backup session info and not agent job.


Also, is it possible to extract the job for each physical machine backup in the last 24 hours.
This is what I use for VM, But not sure how to convert to report for physical machine backup.

#get jobs which run for last 24 hours
$vbrsessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).addhours(-24)}
#get each client from the job
foreach ($session in $vbrsessions) {$session.gettasksessions()| Select Name,Jobname, Status }

soncscy
Veeam Legend
Posts: 504
Liked: 246 times
Joined: Aug 04, 2019 2:57 pm
Full Name: Harvey Carel
Contact:

Re: Report as failed only after 3 retries.

Post by soncscy »

Hi Zaki,

Get-VBRComputerBackupJobSession is from a different "style" where it seems Veeam is more conservative of what each endpoint returns. I'm not sure on the intended method for getting the same, but you can dip into the .NET Methods for this:

[Veeam.Backup.Core.CBackupSession]::GetByJob(uuid JobID)

The method looks to accept a Job UUID, and it accepts the $_.Id for the jobs outputted by Get-VBRComputerBackupJob.

Strangely, while looking for a more "native" solution some of the sessions I have returned by Get-VBRComputerBackupJobSession worked with Get-VBRTaskSession, and some didn't, not sure why. If it works for you, CBackupTaskSession objects have a property JobSess which returns the same as the above, but I'm not sure why some JobSession objects work with this and some don't, as seemingly same type of job works for one job session, not for another.

I think the same method should answer both of your questions though, using the CBackupSession

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests