PowerShell script exchange
Post Reply
walrustiger
Novice
Posts: 3
Liked: never
Joined: Nov 22, 2024 10:34 am
Contact:

Count of backup locations per VM

Post by walrustiger »

I require a count of all backup locations per VM, for example-
Our TestVM is in a Backup Job, Backup Copy Job and Backup Offsite Copy Job

VM Name Locations
TestVM 3

Could any of you powershell gurus help with this?
oleg.feoktistov
Veeam Software
Posts: 2014
Liked: 671 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Count of backup locations per VM

Post by oleg.feoktistov » 1 person likes this post

Hi,

For that purpose I would do something like:

Code: Select all

$vmName = Read-Host -Prompt "Input VM name here"
$vm = Find-VBRViEntity -Name $vmName
$jobs = Get-VBRJob
$total = 0
foreach ($job in $jobs) {
  $object = Get-VBRJobObject -Job $job -Name $vmName
  if ($object.Object.ObjectRef -eq $vm.RawReference) {
    $total += 1
    $backupCopy = $jobs | where {$_.LinkedJobIds -eq $job.Id}
    if ($backupCopy) {
      $total += 1
    }
  }

}

$vm | select Name, @{n='Reference';e={$_.RawReference}}, @{n='JobsCount';e={$total}}
Example above is for vSphere VMs. If you need to make it work for Hyper-V, use Find-VBRHvEntity cmdlet in the second line instead.
The problem might arise if you add whole containers (folders, resource groups etc.) to your jobs instead of VMs directly. In that case, you will need to perform a search a bit backwards - use Find-VBRViEntity or VBR-VBRHvEntity to find a VM and then query and save the containers it belongs to. Then search and correlate objects in jobs based on possible variations of containers that might be added there.

For example, if the most used items in your jobs are "Folder" and "VirtualMachine" (vSphere), I would organize script this way:

Code: Select all

$vmName = Read-Host -Prompt "Input VM name here"
$vm = Find-VBRViEntity -Name $vmName
$jobs = Get-VBRJob
$total = 0
foreach ($job in $jobs) {
  $objects = Get-VBRJobObject -Job $job
  foreach ($object in $objects) {
  if ($object.Object.ViType -eq "Folder") {
    $vms = Find-VBRViEntity -VMsAndTemplates | where {$_.Path -like "*$($object.Name)*" -and $_.Type -eq "Vm"}
    foreach ($item in $vms) {
      if ($item.Name -eq $vmName) {
        $total += 1
            $backupCopy = $jobs | where {$_.LinkedJobIds -eq $job.Id}
            if ($backupCopy) {
                $total += 1
            }
     
      }
    }
  }
  elseif ($object.Object.ViType -eq "VirtualMachine") {
   if ($vm.RawReference -eq $object.Object.ObjectRef) {
    $total += 1
    $backupCopy = $jobs | where {$_.LinkedJobIds -eq $job.Id}
    if ($backupCopy) {
      $total += 1
    }
  }
  }
  }

}

$vm | select Name, @{n='Reference';e={$_.RawReference}}, @{n='JobsCount';e={$total}}
Examples above apply to backup, backup copy and replication jobs.

Best regards,
Oleg
walrustiger
Novice
Posts: 3
Liked: never
Joined: Nov 22, 2024 10:34 am
Contact:

Re: Count of backup locations per VM

Post by walrustiger »

Happy new year all!

I've finally had a chance to try this but am going round in circles and i think chatgpt is stumped too.

I ran that code without "Read-Host -Prompt" as it seems to sit waiting for something (im not sure what)

And I get the following, despite this VM 100% being in at least 2 backup jobs, could anyone steer me in the right direction as I really struggle making heads or tails of these powershell commands when they get this complex.

Name Reference JobsCount
---- --------- ---------
TestVM vm-1071 0

PS. My VMs are added as VMs directly, not folders or resource groups.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests