PowerShell script exchange
Post Reply
StefanNL
Influencer
Posts: 13
Liked: never
Joined: Feb 20, 2019 5:12 pm
Contact:

Need Powershell command to return backup job of VM's that are in folders or use tags

Post by StefanNL »

Hi all,

We have VM's in the backup that are included into jobs in various ways.
They are directly assigned to the job, their folders are included into jobs or they use tags to attach VM's to backup jobs.
I need to be able to find the last job that protected a specific VM by name so that I can use that job name in a VM restore command using powershell.

I think I need to use Get-VBRBackupSession and some other Veeam cdmlet but I can't for the life of me get it to work properly.

Any advice would be greatly appreciated.

Regards,
Stefan
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by david.domask » 1 person likes this post

Hi @StefanNL,

Can I ask, would Get-VBRRestorePoint work for you? This returns COib objects which have a CreationTime property, and you can simply filter the Restore Points based on the VM name, then sort by CreationTime (I prefer -Descending flag on Sort-Object to have most recent returned first), and then pass that COib Object to the VM restore cmdlets.

Can you elaborate on why you want the Job Name? Is it for the "Reason" property maybe? If so, you can use the BackupID property of COib objects to fetch the backup, and on the CBackup Object returned, There is a JobName property you can use to populate some variable for the job name.
David Domask | Product Management: Principal Analyst
StefanNL
Influencer
Posts: 13
Liked: never
Joined: Feb 20, 2019 5:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by StefanNL »

Hi @david.domask thank you for your reply.

All I want to do is start Start-VBRRestoreVM for the VM and use the last available restore point to do so.
Right now I do a

Start-VBRRestoreVM -RestorePoint $restorepoint -Server $host -Resourcepool $resourcepool -Datastore $datastore -VMName <tmp name>

for the restore after filling my restorepoint with:

$restorepoint = Get-VBRRestorePoint -Backup $job -Name $vmname | Select -Last 1

But this doesn't work when you use tags or folders, I'm trying to let my code work with all methods you can attach a VM to a backup job. I realize that things can change between the backup and the restore but I'm just looking for something that works 99,99% of the time.
StefanNL
Influencer
Posts: 13
Liked: never
Joined: Feb 20, 2019 5:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by StefanNL »

I'm trying out using the Get-VBRRestorePoint what I am noticing is that it is taking a VERY long time, it has been running for 6-7 minutes now, still no output. Can I limit the search to the last x number of day's or use some other trick to make it faster?
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by david.domask » 1 person likes this post

Hi @StefanNL,

Get-VBRRestorepoint doesn't concern itself with how the VMs are added to the job, it just looks at what was actually backed up.

Maybe I'm misunderstanding your requirement here, but as I get it, you're wanting to know how to get the last RP for a VM which may be backed up by multiple jobs, am I correct?

If you just pass -Name to the Get-VBRRestorePoint cmdlet, you will get all Restorepoints with that VM name, and it doesn't matter which job its from or how it's added to the job.

Try this sample code and you'll see what I mean:

Code: Select all

function Find-JobByRestorePoint {
	param(
      [Veeam.Backup.Core.COib]$rp
	)
	
	try {$RPBackupID = $rp.GetBackup().GetParent().id}
		catch {$RPBackupID = $rp.BackupID}
  $Backup = Get-VBRBackup |Where-Object {$_.id -eq $RPBackupID}
  $Job = $Backup.JobName
  return $Job
}
Load this function into your active shell session, then do the following:

Code: Select all

$RPs = Get-VBRRestorePoint -Name 'name of some machine you backed up'
$AllRPs = @()
foreach($r in $RPs){
	$JobName = Find-JobByRestorePoint -rp $r
	If($JobName -eq $Null){$JobName = "Imported or Orphaned Backup"}
	$RPInfo = [PSCustomObject]@{
		VMName = $r.VMname
		CreationTime = $r.CreationTime
		Type = $r.Type
		SourceJob = $JobName
	}
	$AllRPs += $RPInfo
}
You should see an output like this from the $AllRPs array:

Code: Select all

PS C:\Users\Administrator> $AllRPs

VMName      CreationTime                Type SourceJob
------      ------------                ---- ---------
ddom-tinyvm 1/19/2023 6:11:44 PM        Full Imported or Orphaned Backup
ddom-tinyvm 3/21/2023 1:40:28 PM    Snapshot Imported or Orphaned Backup
ddom-tinyvm 3/21/2023 1:47:36 PM    Snapshot Imported or Orphaned Backup
ddom-tinyvm 4/10/2023 11:00:00 PM       Full vmware-direct-objstg
ddom-tinyvm 4/11/2023 10:25:55 PM  Increment vmware-direct-objstg
ddom-tinyvm 4/12/2023 10:40:09 PM  Increment vmware-direct-objstg
ddom-tinyvm 4/13/2023 10:05:08 PM  Increment vmware-direct-objstg
ddom-tinyvm 4/14/2023 10:05:26 PM  Increment vmware-direct-objstg
ddom-tinyvm 4/15/2023 10:05:25 PM  Increment vmware-direct-objstg
ddom-tinyvm 4/16/2023 10:05:22 PM  Increment vmware-direct-objstg
As you can see, without the -Backup or -Job flag, Get-VBRRestorePoint I think does what you're aiming for. If you need to know which job the backup is from, the function I wrote will work for you, and you can sort $AllRPs however you like. Since I make a PSCustomObject in my script code, you'll then fetch the desired restorepoint with Get-VBRRestorePoint -ID $idFromAllRPsArray.
David Domask | Product Management: Principal Analyst
StefanNL
Influencer
Posts: 13
Liked: never
Joined: Feb 20, 2019 5:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by StefanNL »

>If you just pass -Name to the Get-VBRRestorePoint cmdlet, you will get all Restorepoints with that VM name, and it doesn't matter which job its from or how it's added to the job.

Okay, but doesn't this also return replication restore points? I only want to see the last backup restore point, I think that might be the reason the original command also used the backup job, something that doesn't work because I can't determine the backup job anymore when the backup job contains folder/tags etc. But if this only returns backups you are 100% correct and my issue is fixed by skipping the whole job part of the Get-VBRRestorePoint cmdlet.

Thanks so far!
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by david.domask » 1 person likes this post

Happy to help!

You are correct, it will return Replica Restore Points, but you can filter them with

Get-VBRRestorePoint -name 'name of vm' | Where-Object {$_.Type -ne "Snapshot"}
David Domask | Product Management: Principal Analyst
StefanNL
Influencer
Posts: 13
Liked: never
Joined: Feb 20, 2019 5:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by StefanNL »

Great, that's what I needed but worked around using the backup job sometime in the past I'm almost sure of this.
This is much better, thank you so much!
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by david.domask »

Always welcome, good luck with the scripting!
David Domask | Product Management: Principal Analyst
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by david.domask »

StefanNL wrote: Apr 17, 2023 12:39 pm I'm trying out using the Get-VBRRestorePoint what I am noticing is that it is taking a VERY long time, it has been running for 6-7 minutes now, still no output. Can I limit the search to the last x number of day's or use some other trick to make it faster?
Also, seems I missed this part. On larger environments, this cmdlet can be slow. The VM name filter should help reduce this considerably though.

If it's really still too long, I'm afraid we need to wait for improved filtering on the cmdlet, but we can use unsupported .NET Reflection to help here. Let me know if -Name time is acceptable and if it's not I'll give you a workaround.
David Domask | Product Management: Principal Analyst
StefanNL
Influencer
Posts: 13
Liked: never
Joined: Feb 20, 2019 5:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by StefanNL »

Using the -name the duration is better (it's faster) but it's taking multiple minutes to find the latest restore point, sometimes it's almost half the duration of the restore when it's a small VM.
It's strange because in the UI it's pretty fast so it seems there might be some optimization opportunity in the PowerShell code.
Thank you for the support yesterday, everything is working now.
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Need Powershell command to return backup job of VM's that are in folders or use tags

Post by david.domask »

Got it, yes, I suppose it can stand a bit of love and care.

As an unsupported workaround (that is safe :) ) you can use the following code. Please note that unsupported means that the Veeam Technical Support Team cannot assist with troubleshooting such code. In this case though it ought be safe for your purposes.

We can get the COib object with .NET Reflection based on the Job Object. Simply pick any job that has the relevant machine in it and do:

$job = Get-VBRJob -name 'name of the job'
$Object = Get-VBRJobObject -Job $job -Name 'name of the relevant machine'
$RPs = [Veeam.Backup.Core.COib]::GetAllOibsByObject($Object.ObjectID)

That should return the same as Get-VBRRestorePoint -Name, and I think you'll find it's a bit faster. It's a bit difficult as you need to fetch the object from _some job_, but after that it should be okay. You might just collect all jobs and then make an array of the results from Get-VBRJobObject, then you can just search that array for your object name and pass that to the .NET Reflection.
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests