PowerShell script exchange
Post Reply
albertwt
Expert
Posts: 640
Liked: 20 times
Joined: Nov 05, 2009 12:24 pm
Location: Sydney, NSW
Contact:

Powershell to list Backup Job name, VMs and its retention?

Post by albertwt » Feb 20, 2019 5:57 am

Hi Experts,

Can anyone here please share the PowerShell script to list Veeam Backup Job name, its VM content and the Retention policy for each backup job?
Any other methods are welcome.

Thank you in advance.
--
/* Veeam software enthusiast user & supporter ! */

Lucas_H
Influencer
Posts: 17
Liked: 4 times
Joined: Nov 16, 2018 3:14 pm
Full Name: Lucas HELLMANN
Location: Lyon, FRANCE
Contact:

Re: Powershell to list Backup Job name, VMs and its retention?

Post by Lucas_H » Feb 20, 2019 9:14 am 1 person likes this post

Hello,

I would have done something like this :

Code: Select all

asnp VeeamPSSNapin
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}
Foreach ($job in $Jobs) {   
    $Objs = $job.GetObjectsInJob()
    $VMList = @()
    Foreach ($obj in $Objs) {
        $VMList=$VMList+$obj.Name
    }
    $Option = $Job.BackupStorageOptions.RetainCycles
    $JobName = $Job.Name
    Write-Host $JobName "`nRetention period : " $Option "`nVM List : " $VMList
}
it may not be the best script but It will do the job.

Cheers,
Lucas.

albertwt
Expert
Posts: 640
Liked: 20 times
Joined: Nov 05, 2009 12:24 pm
Location: Sydney, NSW
Contact:

Re: Powershell to list Backup Job name, VMs and its retention?

Post by albertwt » Feb 20, 2019 11:17 am

Many thanks Lucas,

Would it be possible to list the size of the backup with the script below and the Backup Repository it is located?

Code: Select all

Get-VBRJob | ?{$_.JobType -eq "Backup"} | %{
	$JobName = $_.Name
	$_ | Get-VBRJobObject | ?{$_.Object.Type -eq "VM"} | Select @{ L="Job"; E={$JobName}}, Name, @{ L="Size"; E={$_.ApproxSizeString}} | Sort -Property Job, Name
}
--
/* Veeam software enthusiast user & supporter ! */

Lucas_H
Influencer
Posts: 17
Liked: 4 times
Joined: Nov 16, 2018 3:14 pm
Full Name: Lucas HELLMANN
Location: Lyon, FRANCE
Contact:

Re: Powershell to list Backup Job name, VMs and its retention?

Post by Lucas_H » Feb 20, 2019 1:40 pm

Hello,

Did some modification with the script I posted and what you posted :

Code: Select all

asnp VeeamPSSnapin
Get-VBRJob | ?{$_.JobType -eq "Backup"} | %{
    
    $job = $_
    $JobName = $_.Name
    $Backup = Get-VBRBackup -Name $JobName
    $lastsession = $job.FindLastSession()
    $Session = $job.FindLastSession()
    foreach($tasksession in $lastsession.GetTaskSessions()) {
    $PointsOnDisk = (get-vbrbackup -Name $job.Name | Get-VBRRestorePoint -Name $tasksession.Name | Measure-Object).Count 
    $BackupTotalSize = [math]::round($Session.Info.Progress.TotalUsedSize/1Gb,2)
    $BackupSize = [math]::round($Session.Info.BackedUpSize/1Gb,2)
    $RepositoryPath = $Backup.Info.DirPath.ToString()
    $LastBackupStart = $Session.CreationTime
    $LastResult = $job.GetLastResult()
    }
	$_ | Get-VBRJobObject | ?{$_.Object.Type -eq "VM"} | Select @{ L="Job"; E={$JobName}}, Name, @{ L="Size"; E={$_.ApproxSizeString}}, @{ L="PointsOnDisk"; E={$PointsOnDisk}}, @{ L="LastResult"; E={$LastResult}}, @{ L="LastBackupStart"; E={$LastBackupStart}}, @{ L="LastBackupTotalSize"; E={$BackupTotalSize}}, @{ L="LastBackupSize"; E={$BackupSize}}, @{ L="RepositoryPath"; E={$RepositoryPath}} | Sort -Property Job, Name
}
This script check for each VM in a Backup Job :
  • The VMSize
  • The number of restore points available on disk
  • The last backup result and the start Time
  • The last backup : total size and data copied on disk
  • And the repository path (I'm not sure if this is what you wanted ?)
To note : It is pretty slow (around 10 seconds per Vm).
I didn't look much further to optimize it, I think the slow point is when it checks for the repository path, the two foreach doesn't help much either as I'm far from being a powershell expert.

Tell me if this works for you :)

Cheers,
Lucas

albertwt
Expert
Posts: 640
Liked: 20 times
Joined: Nov 05, 2009 12:24 pm
Location: Sydney, NSW
Contact:

Re: Powershell to list Backup Job name, VMs and its retention?

Post by albertwt » Feb 20, 2019 10:27 pm

Lucas,

Thank you for your help in this matter, somehow it is returning the below error instead of any result?
You cannot call a method on a null-valued expression.
At line:15 char:9
+ $RepositoryPath = $Backup.Info.DirPath.ToString()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Which refers to this line:
$RepositoryPath = $Backup.Info.DirPath.ToString()
--
/* Veeam software enthusiast user & supporter ! */

Lucas_H
Influencer
Posts: 17
Liked: 4 times
Joined: Nov 16, 2018 3:14 pm
Full Name: Lucas HELLMANN
Location: Lyon, FRANCE
Contact:

Re: Powershell to list Backup Job name, VMs and its retention?

Post by Lucas_H » Feb 21, 2019 8:10 am

Hello,

Yes I had this problem on some backup too, I had this solved but I didn't post the up to date one.
This is because you may have changed the job name at some point but the backup name didn't change with it.
In my case : I had job like : Job-CLIENT before and now I have Job-CLIENT-HPV or (VMW depending on the infrastucture). So If you are in a similar situation, the script below might work.

To correct it I added a little if loop that check if the Backup here is null : $Backup = Get-VBRBackup -Name $JobName
If it is empty, I know that in my case I will have to crop the end to get the right backup corresponding to the job.
Here the code to add just after $Backup = Get-VBRBackup -Name $JobName

Code: Select all

if ($Backup -eq $null) {
        $BackupName = $JobName.Remove(7,4) #The number are for my case, you might changed it 
        $Backup = Get-VBRBackup -Name $BackupName    }
 else {}
Another solution would be to rename the Job to the correct Backup name, it should work too.

Cheers,
Lucas

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests