PowerShell script exchange
Post Reply
walrustiger
Lurker
Posts: 1
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: 2010
Liked: 669 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
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests