PowerShell script exchange
Post Reply
ThierryF
Expert
Posts: 129
Liked: 34 times
Joined: Mar 31, 2018 10:20 am
Contact:

Scripting help ...

Post by ThierryF »

Hello,

Need you help with this script :

I am gathering my backups to process details.
Unfortunately, it does not list me Agent backup infos ...
Where am I wrong ?

Code: Select all

# Getting Restore points
$VBRRestorePoint = Get-VBRRestorePoint | where {((Get-Date) - ($_.CreationTime)).totaldays -le "2"} ;

### (at this stage, I retrieve agent backup restore points)
					
# Getting Backup Jobs
$VBRJOBS = [Veeam.Backup.Core.CBackupJob]::GetAll()

foreach ($VBRJOB in $VBRJOBS) {

	$AllJobSessions = @()
	
	#Retrieving session for given backup job
	$JobSessions = [Veeam.Backup.Core.CBackupSession]::GetByJob($VBRJOB.Id) 
						
	if ([bool]$JobSessions){
		foreach ($CurrentSession in $JobSessions) {
			if (((Get-Date) - ($CurrentSession.creationtime)).totaldays -le "2") {   
					$null=$AllJobSessions.Add($CurrentSession.id)
			}
		}
	}
        
       ### (at this stage, I got Session Ids of all of my backups, vmware based or agent based)
	
	$UniqueJobSessions = $AllJobSessions | Select-Object -Unique 
					
	foreach ($CurrentSessionID in $UniqueJobSessions) {
		
		# Getting tasks in given backup session
		$TaskSessions = Get-VBRTaskSession ($VBRSession)

		## Problems start here for agent backups ...

		#Processing task session in task sessions
		foreach ($TaskSession in $TaskSessions) {
			$TSNAME        = $TaskSession.name ;
			$TSPLATFORM    = $TaskSession.Info.ObjectPlatform;
			$TSSTATUS      = $TaskSession.Status;
			$TSPROGRESS    = $TaskSession.progress;
			$TSCT          = $TSPROGRESS.starttimelocal.tostring("$NEWDATEFORMAT");
			$TSET          = $TSPROGRESS.stoptimelocal.tostring("$NEWDATEFORMAT");
			$TSDUR         = $TSPROGRESS.stoptimelocal - $TSPROGRESS.starttimelocal ; if ($TSDUR -lt 0) {$TSDUR="N-A";$TSET="N-A"} ;
			$TSSPEED       = $TSPROGRESS.AVGSPEED;
			$TSTRANSFERED  = $TSPROGRESS.TransferedSize;
			$TSREADSIZE    = $TSPROGRESS.ReadSize;
			$TSTOTALGB     = $TSPROGRESS.ProcessedSize;
			$TSTOTALUSEDGB = $TSPROGRESS.ProcessedUsedSize;
                        (...)
		}
	}
}
For agent backups, Get-VBRTaskSession does not list different task sessions, preventing me to grab backups infos.

Any Idea what's wrong ?

Thanks

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

Re: Scripting help ...

Post by oleg.feoktistov »

Hi ThierryF,

Please remember to wrap your scripts in code tags (</> label in full editor mode). They make it much more readable and allow to copy it to our labs in one click.

As for the issue, since you went with unsupported .NET methods, policies managed by agent should have been displayed already with your script as is. So, I'm going to assume that you need to retrieve agent jobs managed by backup server. In this case and overall with any agent job I would recommend using dedicated cmdlets for agent jobs and sessions. The tricky part is agent jobs managed by vbr have slightly different architecture, so you need to retrieve child backup sessions first to get task sessions. For example:

Code: Select all

$jobs = Get-VBRComputerBackupJob
$sessions = Get-VBRComputerBackupJobSession
$totalTaskSessiions = @()
foreach ($job in $jobs) {
  $jobSessions = $sessions | where {$_.JobId -eq $job.Id}
  foreach ($session in $jobSessions) {
    if ($job.Mode -eq 'ManagedByBackupServer') {
    $sessionObject = [Veeam.Backup.Core.CBackupSession]::Find($session.Id)
    $child = $sessionObject.GetChildSessions()
    $session = [Veeam.Backup.Core.CBackupSession]::Find($child.Id)
    }
    $taskSessions = Get-VBRTaskSession -Session $session
    $taskSessions
  }
}
For policies managed by agents task sessions are returned as is. That is why in the script I'm checking if job is in ManagedByBackupServer mode.

Also, some comments about the other parts of the script:

Code: Select all

	if ([bool]$JobSessions){
		foreach ($CurrentSession in $JobSessions) {
			if (((Get-Date) - ($CurrentSession.creationtime)).totaldays -le "2") {   
					$null=$AllJobSessions.Add($CurrentSession.id)
			}
		}
	}
Here I don't understand why do you add $CurrentSession.Id to $AllJobSessions array instead of a full object. You are going to need it later for Get-VBRTaskSession.

Code: Select all

$TaskSessions = Get-VBRTaskSession ($VBRSession)
Here it looks like $VBRSession variable was never declared.

Code: Select all

$TSTOTALUSEDGB = $TSPROGRESS.ProcessedUsedSize;
(...)
Here I don't know what you meant by (...) operator. It looks like you wanted to group something, but it looks empty.

FYI, without fixing the points above the script didn't work in my lab.


Hope it helps,
Oleg
ThierryF
Expert
Posts: 129
Liked: 34 times
Joined: Mar 31, 2018 10:20 am
Contact:

Re: Scripting help ...

Post by ThierryF » 1 person likes this post

Hello Oleg,

Thanks for reply.
(...) and the undeclared variable you pointed out are snipped code to make the explanation simpler.

I will give a look at your suggestions about Get-VBRComputerBackupJob

Thanks for your time and kind assistance !

Cheers

Th
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests