PowerShell script exchange
Post Reply
robertjray
Novice
Posts: 7
Liked: never
Joined: Sep 23, 2011 1:32 pm
Full Name: Robert Ray
Contact:

Powershell script to list all restore points per job

Post by robertjray »

Specifically I would like to list all restore points for each VM for all cloud copy jobs. If someone has the method to list all VM restore points in a job I can work through the for each for the specific jobs of interest.

I have got scripting working to list all VM's in each job. I have the scripting to list all restore point by vm, but what I couldn't work out was the get-vbrrestorepoint -name and limit that to just those within a specific job or a list of jobs.
robertjray
Novice
Posts: 7
Liked: never
Joined: Sep 23, 2011 1:32 pm
Full Name: Robert Ray
Contact:

Re: Powershell script to list all restore points per job

Post by robertjray »

Below is the current one I am using. I would like to add a foreach (VM in the job) loop into this. So reporting would end up being Job: "Job1" VM "VM1" - RestorePoint list. Additionally, if I could restrict the job to a specific Job type i.e. cloud copy jobs that is my ultimate goal.

Code: Select all

$backups = get-vbrbackup
$allstorages = @()

foreach ($backup in $backups) {
  foreach ($storage in $backup.getallstorages()) {
   $allstorages += New-Object -TypeName psobject -Property @{
    JobName=$backup.Name;
    Time=$storage.CreationTime;
    Path=$storage.Info.FilePath;
    BackupSizeGB=$storage.Info.Stats.BackupSize/1GB;
    DataSizeGB=$storage.Info.Stats.DataSize/1GB;
    }
  }
}
$allstorages = $allstorages | Sort-Object -Property JobName,Time
$allstorages | select -property Jobname,Time,Path,BackupSizeGB,DataSizeGB | ConvertTo-Csv -Delimiter "," > c:\export.csv
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Powershell script to list all restore points per job

Post by tsightler »

To find only cloud backup jobs created from Backup Copy jobs you can filter the output of Get-VBRBackup like this:

Code: Select all

Get-VBRBackup | ?{$_.IsCloudBackup -eq $true -and $_.JobType -eq "BackupSync"}
Unfortunately, I wasn't sure I followed your request from that point. I think what you may want is $storage.GetOibs(), which will get you the VM names, point creation time and such, but what wasn't clear was how you wanted the output formatted since you are exporting to CSV. Can you provide a mockup of what that CSV would look like and then we can determine the easiest way to get the data in the formatting you are looking for.
robertjray
Novice
Posts: 7
Liked: never
Joined: Sep 23, 2011 1:32 pm
Full Name: Robert Ray
Contact:

Re: Powershell script to list all restore points per job

Post by robertjray »

Thanks Tom. That gave us a big piece of the puzzle. I think our DevOPS guy Tyler can now get second output file listing by VM by job all the restorepoints. When we get it doing what we want, I will respond with the finished code.
robertjray
Novice
Posts: 7
Liked: never
Joined: Sep 23, 2011 1:32 pm
Full Name: Robert Ray
Contact:

Re: Powershell script to list all restore points per job

Post by robertjray »

Just about there. Something with this is causing a loop or something. In a very isolated environment it will run but when I open try and do it in production it appears to just run infinitely. Do you see anything that jumps out or a way to improve the execution.

Code: Select all

Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$backups = get-vbrbackup | ?{$_.IsCloudBakcup -eq $true -and $_.JobType -eq "BackupSync"}
$allstorages = @()
$allRestorePoints = @()

foreach ($backup in $backups) { 
    foreach ($storage in $backup.getallstorages()) {
            $allstorages += New-Object -TypeName psobject -Property @{
            JobName=$backup.Name;
            Time=$storage.CreationTime;
            Path=$storage.Info.FilePath;
            BackupSizeGB=$storage.Info.Stats.BackupSize/1GB;
            DataSizeGB=$storage.Info.Stats.DataSize/1GB;
            }
            foreach($vm in $storage.GetOibs()){
                $points = Get-VBRRestorePoint | ?{$_.Name -eq $vm.Name -and $_.BackupId -eq $backup.Id}
                $allRestorePoints += New-Object -TypeName psobject -Property @{
                JobName=$backup.Name;
                VMName=$vm.Name;
                CreateTime=$points.CreationTime;
                Type=$points.Type;
                #True means complete (State==OK) False means incomplete (State==Incomplete)
                State=$points.IsConsistent;
                }
            }
        }
}
$allRestorePoints = $allRestorePoints | Sort-Object -Property JobName,CreateTime
$allRestorePoints | select -Property JobName,CreateTime,VMName,Type,State | ConvertTo-Csv -Delimiter "," > c:\vms.csv
$allstorages = $allstorages | Sort-Object -Property JobName,Time
$allstorages | select -property Jobname,Time,Path,BackupSizeGB,DataSizeGB | ConvertTo-Csv -Delimiter "," > c:\export.csv
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Powershell script to list all restore points per job

Post by tsightler »

robertjray wrote:Do you see anything that jumps out or a way to improve the execution.
Absolutely, you have Get-VBRRestorePoint inside a loop. Get-VBRRestorePoint, without any parameters, pulls every single restore point in the entire environment so it's a very heavy command in general and, even with parameters, you don't want to run it hundreds of times. Easy fix, filter it down to only the needed restore points. You only need the restore points for the backups that are going to be processed which, in this case, is just Backup Copy jobs to Cloud repos. Since you're grabbing those backups at the start of the script you can then just pipe that to Get-VBRRestorePoint and store the results in a variable, moving all that heavy work well outside of the loop. That should drastically reduce the run time.

BTW, you also had a typo (IsCloudBakcup instead if IsCloudBackup). I also think you're still going to have an issue with the ConvertTo-CSV because $allRestorePoints includes properties that are arrays, so I don't see how, if you have more than one restore point for a VM, you won't just end up with output that says "System.Object[]" instead of the actual data. Perhaps you can pipe to a select with some expressions to overcome that.

Anyway, here's the code where I fixed the typo and the performance issue (I think), but not the output problem that I think you will have, hopefully that will get you closer:

Code: Select all

Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
$backups = get-vbrbackup | ?{$_.IsCloudBackup -eq $true -and $_.JobType -eq "BackupSync"}
$restorepoints = $backups | Get-VBRRestorePoint
$allstorages = @()
$allRestorePoints = @()

foreach ($backup in $backups) { 
    foreach ($storage in $backup.getallstorages()) {
            $allstorages += New-Object -TypeName psobject -Property @{
            JobName=$backup.Name;
            Time=$storage.CreationTime;
            Path=$storage.Info.FilePath;
            BackupSizeGB=$storage.Info.Stats.BackupSize/1GB;
            DataSizeGB=$storage.Info.Stats.DataSize/1GB;
            }
            foreach($vm in $storage.GetOibs()){
                $points = $restorepoints | ?{$_.Name -eq $vm.Name -and $_.BackupId -eq $backup.Id}
                $allRestorePoints += New-Object -TypeName psobject -Property @{
                JobName=$backup.Name;
                VMName=$vm.Name;
                CreateTime=$points.CreationTime;
                Type=$points.Type;
                #True means complete (State==OK) False means incomplete (State==Incomplete)
                State=$points.IsConsistent;
                }
            }
        }
}
$allRestorePoints = $allRestorePoints | Sort-Object -Property JobName,CreateTime
$allRestorePoints | select -Property JobName,CreateTime,VMName,Type,State | ConvertTo-Csv -Delimiter "," > c:\vms.csv
$allstorages = $allstorages | Sort-Object -Property JobName,Time
$allstorages | select -property Jobname,Time,Path,BackupSizeGB,DataSizeGB | ConvertTo-Csv -Delimiter "," > c:\export.csv
robertjray
Novice
Posts: 7
Liked: never
Joined: Sep 23, 2011 1:32 pm
Full Name: Robert Ray
Contact:

Re: Powershell script to list all restore points per job

Post by robertjray »

Thanks Tom. I already had fixed the cloudbakcup typo but the other stuff is good information. This is beyond my abilities so Tyler has taken this over for me. Also, you are correct that we were seeing random "system.object" outputs with it randomly giving us good output.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Powershell script to list all restore points per job

Post by tsightler »

There's actually some other problems with this script as well, it ends up looping through each VM multiple times and creating identical lines. As a super hackish fix for this issue, as well as the output issue I mentioned above, I modified the output lines for $allRestorePoints to the following:

Code: Select all

$allRestorePoints = $allRestorePoints | Sort-Object -Property JobName,CreateTime -Unique
$allRestorePoints | select -Property JobName,@{N="CreateTime";E={[string]::join(“;”, ($_.CreateTime))}},VMName,@{N="Type";E={[string]::join(";", ($_.Type))}},@{N="State";E={[string]::join(";", ($_.State))}} | ConvertTo-Csv -Delimiter "," > c:\vms.csv
This expands all of the array elements with a semicolon delimited string value, while the properties are still comma delimited. I have no idea what you're wanting to use the script output for (for example, if it's for Excel, there's a better fix) so I don't know if that will work, but it could very easily be modified to comma or space delimit the array fields instead (just replace the semicolon in the join expressions). If you provide a simple example of what you'd want it to look like, I'm sure we can get there.

It would be better to fix the logic to not run mulitple times for each VM and, when I have more than a couple of minutes, I'll take a look, but I suspect this will get really close to producing the output you want, at least from a data perspective, and run fairly quickly, even with the extra loops. If Tyler needs any other help or has other questions, he can feel free to continue to post here or you can pass him my contact info and I'll be happy to help.
robertjray
Novice
Posts: 7
Liked: never
Joined: Sep 23, 2011 1:32 pm
Full Name: Robert Ray
Contact:

Re: Powershell script to list all restore points per job

Post by robertjray »

Tom,

That runs great and it quick. The only challenge it is now puts all the restorepoints for a VM in a single record so I have to figure out a way to break that out and align the date and status with the correct restore point.
tdewin
Veeam Software
Posts: 1818
Liked: 655 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Re: Powershell script to list all restore points per job

Post by tdewin » 1 person likes this post

well I would assume you could pipe $allRestorePoints to group-object -property VMname

Code: Select all

$groupedbyvm = $allRestorePoints | Group-Object -Property vmname
foreach($vm in $groupedbyvm) { 
  write-host $vm.name; 
  foreach( $point in $vm.Group) { 
   write-host $point.CreateTime 
  }  
}
I think it would be safer to add the internal id to the original new object and filter on that to be sure that you have the same VM even if the name was "replaced"

Code: Select all


foreach ($backup in $backups) { 
    foreach ($storage in $backup.getallstorages()) {
            $allstorages += New-Object -TypeName psobject -Property @{
            JobName=$backup.Name;
            Time=$storage.CreationTime;
            Path=$storage.Info.FilePath;
            BackupSizeGB=$storage.Info.Stats.BackupSize/1GB;
            DataSizeGB=$storage.Info.Stats.DataSize/1GB;
            }
            foreach($vm in $storage.GetOibs()){
                $points = $restorepoints | ?{$_.Name -eq $vm.Name -and $_.BackupId -eq $backup.Id}
                $allRestorePoints += New-Object -TypeName psobject -Property @{
		VMObjectId=$vm.ObjectId
                JobName=$backup.Name;
                VMName=$vm.Name;
                CreateTime=$points.CreationTime;
                Type=$points.Type;
                #True means complete (State==OK) False means incomplete (State==Incomplete)
                State=$points.IsConsistent;
                }
            }
        }
}
and then group by vmobjectid

Code: Select all

$groupedbyvm = $allRestorePoints | Group-Object -Property VMObjectId
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Powershell script to list all restore points per job

Post by tsightler » 2 people like this post

That's why I was asking for a sample of what you want the output to look like. I could think of a number of ways to format this, but since I don't know what you're trying to achieve with the output, it's hard to know the best way. If you had a mockup of what you wanted the output to look like I'm should it wouldn't be difficult to create code to format it that way. Just let me know if you need anything.
robertjray
Novice
Posts: 7
Liked: never
Joined: Sep 23, 2011 1:32 pm
Full Name: Robert Ray
Contact:

Re: Powershell script to list all restore points per job

Post by robertjray »

I think Tyler can take the groupby recommendations and help me get the output I need. Thanks a whole lot for this.

For now I was able to tweak the output to get a report of what I was looking for. Here is a sample of what I made.

http://i66.tinypic.com/207tjqg.jpg
DarellCraighead
Lurker
Posts: 1
Liked: never
Joined: Sep 23, 2020 4:50 pm
Full Name: Darell Craighead
Contact:

Re: Powershell script to list all restore points per job

Post by DarellCraighead »

Amazing - I think this script may give me what I need. That said, it isn't working.

My use case is ALL restore points - not just cloud. I changed line 2 $backups = get-vbrbackup and that seems to work.

When I run the script completes but no data in either csv.

Working through the script I'm finding $backup.getallstorages() returns nothing.
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Powershell script to list all restore points per job

Post by david.domask »

Hi Darell,

The internal (unsupported) method indeed changed to GetAllChildrenStorages().

Try the following edited script, it should work.

Code: Select all

$backups = get-vbrbackup
$restorepoints = $backups | Get-VBRRestorePoint
$allstorages = @()
$allRestorePoints = @()

foreach ($backup in $backups) { 
    foreach ($storage in $backup.getallchildrenstorages()) {
				$allstorages += New-Object -TypeName psobject -Property @{
					JobName=$backup.Name;
					Time=$storage.CreationTime;
					Path=$storage.Info.FilePath;
					BackupSizeGB=$storage.Info.Stats.BackupSize/1GB;
					DataSizeGB=$storage.Info.Stats.DataSize/1GB;
            }
            foreach($vm in $storage.GetOibs()){
					#$points = $restorepoints | ?{$_.Name -eq $vm.Name -and $_.BackupId -eq $backup.Id} #not necessary/working it seems
					$allRestorePoints += New-Object -TypeName psobject -Property @{
						JobName=$backup.Name;
						VMName=$vm.Name;
						CreateTime=$vm.CreationTime;
						Type=$vm.Type;
						#True means complete (State==OK) False means incomplete (State==Incomplete)
						#State=$points.IsConsistent;
                }
            }
        }
}
$allRestorePoints = $allRestorePoints | Sort-Object -Property JobName,CreateTime
$allRestorePoints | select -Property JobName,CreateTime,VMName,Type,State | ConvertTo-Csv -Delimiter "," > c:\vms.csv
$allstorages = $allstorages | Sort-Object -Property JobName,Time
$allstorages | select -property Jobname,Time,Path,BackupSizeGB,DataSizeGB | ConvertTo-Csv -Delimiter "," > c:\export.csv
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests