PowerShell script exchange
Post Reply
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

List VM of a CopyJob

Post by GianlucaCroci »

Good morning,

I wanted to ask how I can do to extract a list of VMs from a CopyJob.
The problem is that the CopyJob contains several backup jobs. Therefore it has to extract a second level list, because inside the copyjob there're jobs and not VMs directly.
Unfortunately I've not found how to extract this list.

The lists should be with "VM name", "last saved (date)", "size".

If you also manage to have another list with "VM Name", "ALL the saves (data)", "size of each save", would be fantastic.


Thanks in advance for your help
Kind regards
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: List VM of a CopyJob

Post by oleg.feoktistov »

Hi Gianluca,

Here is the script to get you started on the first list:

Code: Select all

$job = Get-VBRJob -Name 'Backup Copy Job 1'
$linkedJobs = Get-VBRJob | where {$_.Id -eq $job.LinkedJobIds.Guid}
foreach ($linkedJob in $linkedJobs) {
$objects = Get-VBRJobObject -Job $linkedJob
$backup = Get-VBRBackup -Name $job.Name
$rp = Get-VBRRestorePoint -Backup $backup
$session = Get-VBRBackupSession -Name "$($linkedJob.Name)*" | Sort-Object EndTime | select -Last 1
foreach ($object in $objects) {
  $rp = $rp | where {$_.Name -eq $object.Name} | sort CreationTime | select -Last 1
  $storage = $rp.GetRestoreStorages()
  $backupSize = [Math]::Round($storage.Stats.BackupSize / 1GB, 1)
  $task = Get-VBRTaskSession -Session $session -Name $object.Name
  $object | select Name, @{n='LastSaved';e={$task.Progress.StopTimeUtc}}, @{n='Size';e={$backupSize}}
 }
}
With few tweaks to it there will be no problems to form the second list you asked.

Thanks,
Oleg
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

Hello Oleg,
thank's a lot.

I've changed the name of the job on the first row, but when I run the Script, on the second row, I've a Warning.
WARNING: This cmdlet is no longer supported for computer backup jobs. Use "Get-VBRComputerBackupJob" instead.
and the script end without result.
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: List VM of a CopyJob

Post by oleg.feoktistov »

Hello Gianluca,

What is the mode of backup copy job you are after? Is it immediate copy or periodic? What platform is acting as a source? Is it Vmware, Hyper-V, agents or else?

Thanks,
Oleg
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

Hello Oleg,

it's a CopyJob with the "Copy mode" = "Immediate copy" and inside I've added few backup's jobs of vSphere Server.


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

Re: List VM of a CopyJob

Post by oleg.feoktistov »

Please try this script instead. Should work for immediate backup copy jobs:

Code: Select all

$job = Get-VBRJob -Name 'Simple Backup Copy Job'
$linkedJobs = Get-VBRJob -WarningAction SilentlyContinue | where {$_.Id -eq $job.LinkedJobIds.Guid}
$backup = Get-VBRBackup -Name 'Simple Backup Copy Job'
$childBackup = $backup.FindChildBackups()
$rps = Get-VBRRestorePoint -Backup $childBackup
foreach ($linkedJob in $linkedJobs) {
$objects = Get-VBRJobObject -Job $linkedJob
$session = Get-VBRBackupSession -Name "$($linkedJob.Name)*" | Sort-Object EndTime | select -Last 1
foreach ($object in $objects) {
  $rp = $rps | where {$_.Name -eq $object.Name} | sort CreationTime | select -Last 1
  $storage = $rp.FindStorage()
  $backupSize = [Math]::Round($storage.Stats.BackupSize / 1GB, 2)
  $task = Get-VBRTaskSession -Session $session -Name $object.Name
  $object | select Name, @{n='LastSaved';e={$task.Progress.StopTimeUtc}}, @{n='Size';e={$backupSize}}
 }
}
The difference is that you need to query child backup first. I also changed method for getting storages (backup files).
The warning you see on Get-VBRJob invocation can be avoided with -WarningAction SilentlyContinue option like I did in the script above.

Hope it helps,
Oleg
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

Hello Oleg,

sorry but I don't get any results.
if I execute one line at a time, in the second line (the one that gave me the Warning before) I have no results.
in the first line instead I get 8 lines.

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

Re: List VM of a CopyJob

Post by oleg.feoktistov »

Hello Gianluca,

Got it. I fixed it in my script and tested with several jobs linked to a backup copy job in immediate mode, it worked.
Please change the code between 1st and 3rd lines to the following snippet:

Code: Select all

$linkedJobs = @()
foreach ($id in $Job.LinkedJobIds.Guid) {
$linked = Get-VBRJob -WarningAction SilentlyContinue | where {$_.Id -eq $id}
$linkedJobs += $linked
}
Thanks,
Oleg
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

Good Morning Oleg,

now I've a result.

For some servers I've this message
You cannot call a method on a null-valued expression.
At line:16 char:3
+ $storage = $rp.FindStorage()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
related to "$storage = $rp.FindStorage()".

How can I save the result in a text usable? without the column header each time? :-)

Thanks again
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

added
| Export-csv "D:\scripts\file_export.csv" -NoTypeInformation -Append
at the end of the row "$object | select Name..."

it's right or there's a better solution?

thanks again
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: List VM of a CopyJob

Post by david.domask »

GianlucaCroci wrote: Mar 30, 2022 5:48 am Good Morning Oleg,

now I've a result.

For some servers I've this message



related to "$storage = $rp.FindStorage()".

How can I save the result in a text usable? without the column header each time? :-)

Thanks again
Message is telling you exactly the issue ;) $rp is $null, meaning you have some jobs that never produced successful backups likely, and when you try to call FindStorage() on an empty value, it throws a Null Reference Error.

Write some logic to check if $rp is empty or not and skip processing of such points.

As for exporting it, CSV certainly works. You can also dump it to file with the Add-Content cmdlet from Powershell, but you need to play with it a bit.
David Domask | Product Management: Principal Analyst
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

If i run the script on a Copyjob of different servers, it gives me a list but partial (not all server of this Copyjob are listed).
I then took another Copyjob, with fewer servers, to verify the execution.
In this Copyjob I should have "MariaDB" servers, but instead show me "Mongo" ones.
Up to the line
$objects = Get-VBRJobObject -Job $linkedJob
all good, but when I run this line I get precisely the "Mongo" servers.
I tried to insert the Copyjob name in place of $linkedJob, as well as that of the "MariaDB Backup Job", but the result doesn't change.

I just don't understand what's wrong with it.
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: List VM of a CopyJob

Post by david.domask »

Can you maybe show what you run in the shell (screenshot and upload it to some image host), and maybe show the Backups under Backups > Disk (Copy)?

That the name didn't work makes me think it's about the processing of immediate copy jobs as they have a unique naming structure and have to be handled a bit differently.
David Domask | Product Management: Principal Analyst
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

execution FULL
Image

execution partialy
Image

execution partialy but with the row "Get-VBRJobObject -Job $linkedJob"
Image

execution partialy but with the row "Get-VBRJobObject -Job "MariaDB-Weekly_Linux" "
Image

last execution of the backup job "MariaDB-Weekly_Linux"
Image
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: List VM of a CopyJob

Post by oleg.feoktistov »

Hi Gianluca,

From what it looks like on the screenshots you shared, the problem might be with the job objects of VssChild type displayed upon Get-VBRJobObject execution. These are just for guest processing options and it most likely means that there is another level you adjusted them on. For instance, you backup a cluster, but also add vss options for a resource pool inside this cluster. Otherwise, the cluster would be recognized as of Include type only. So, if you exclude such job objects from processing, the script will stop searching for restore points for them. Inside the foreach ($object in $objects) loop try to wrap everything in the following if statement and see if it works:

Code: Select all

if ($object  -ne 'VssChild') {
  $rp = $rps | where {$_.Name -eq $object.Name} | sort CreationTime | select -Last 1
  $storage = $rp.FindStorage()
  $backupSize = [Math]::Round($storage.Stats.BackupSize / 1GB, 2)
  $task = Get-VBRTaskSession -Session $session -Name $object.Name
  $object | select Name, @{n='LastSaved';e={$task.Progress.StopTimeUtc}}, @{n='Size';e={$backupSize}}
  }
Thanks,
Oleg
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

Good morning Oleg,

same result.


used breakpoints, and here more info

Image

Image

Image

RESULT
Image

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

Re: List VM of a CopyJob

Post by oleg.feoktistov »

Good morning Gianluca,

My bad, I must have forgotten to input $object.Type in the if statement. Should be like that:

Code: Select all

if ($object.Type  -ne 'VssChild') {
  
}
Please try with the above and let me know how it goes.

Thanks,
Oleg
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

Thanks a lot Oleg.

This is the last run. I've only the name of the Backup Jobs inserted into the CopyJob, and no Server name.

Image

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

Re: List VM of a CopyJob

Post by oleg.feoktistov »

One another case I have in mind is it happens if there are no backups created for one of the source backup job.
Try to wrap storage query in not null validation like this:

Code: Select all

 if ($rp -ne $null) {
  $storage = $rp.FindStorage()
  $backupSize = [Math]::Round($storage.Stats.BackupSize / 1GB, 2)
  $task = Get-VBRTaskSession -Session $session -Name $object.Name
  $object | select Name, @{n='LastSaved';e={$task.Progress.StopTimeUtc}}, @{n='Size';e={$backupSize}}
  }
Also, in the beginning of the script you could save time a bit invoking Get-VBRJob one time instead of two:

Code: Select all

$jobs = Get-VBRJob -WarningAction SilentlyContinue
$copyjob = $jobs | where {$_.Name -eq 'Simple Backup Copy Job'}
$linkedJobs = @()
foreach ($id in $copyjob.LinkedJobIds.Guid) {
$linked = $jobs | where {$_.Id -eq $id}
$linkedJobs += $linked
}
Thanks,
Oleg
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

Hello Oleg,

I get the same result unfortunately.

I'll proceed in another way. I'll do an export from the Enterprise manager in Excel, with some manipulations to remove the "Tapes" and "CopyJobs".
I'd like to have a script, but since it gets too complex, I prefer to abandon this path and leave you time for other posts.

Thanks you for your effort and your time.
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: List VM of a CopyJob

Post by oleg.feoktistov »

Hello Gianluca,

It's unfortunate that it keeps throwing the same error, but it only means that we didn't get the root cause of your specific case, where $rp variable is null.

Please let me know if you decide to get back to this script in the future.

Thanks,
Oleg
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: List VM of a CopyJob

Post by GianlucaCroci »

Hello Oleg,

sorry for the late reply.

Thanks a lot for your help. If we'll decide to get back, I'll wirte here.

Thanks you again for your effort and your time.
Kind regards
Gianluca
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests