PowerShell script exchange
Post Reply
rafawi
Lurker
Posts: 1
Liked: never
Joined: Dec 13, 2023 2:44 pm
Full Name: Rafal
Contact:

[Powershell] Effectiveness of backup for individual VMs

Post by rafawi »

I would like to make a report using a powershell script that shows what percentage of backups success for selected VMs in the job (we do it once a day with 3 repetitions, if there is a failure). I am currently stuck at this stage:

Code: Select all

if ($job -eq $null) {
      Write-Host "The job named '$jobName' does not exist"
      } 
      else 
      {
      # Get information about the virtual machines in the job
      $vmsInJob = $job.GetObjectsInJob()

      if ($vmsInJob.Count -gt 0) {
          Write-Host "VMs in job '$jobName':"
          $vmsInJob | ForEach-Object {
              $vmName = $_.Name
              # Get the backup sessions for a given virtual machine
              $backupSessionsForVM = Get-VBRBackupSession

              # Specify the time range for November
              $startDate = Get-Date -Day 1 -Month 11 -Year 2023 -Hour 0 -Minute 0 -Second 0
              $endDate = Get-Date -Day 1 -Month 12 -Year 2023 -Hour 0 -Minute 0 -Second 0

              # Filter backup sessions for a given VM based on time range and "Success" status
              $successfulBackupsForVM = $backupSessionsForVM | Where-Object 
              {
                $_.EndTime -ge $startDate -and $_.EndTime -lt $endDate -and $_.Result -eq "Success" -and $_.IsFullMode -eq $True 
              } 
              | Sort-Object EndTime -Descending


              # Display the number of successful full backups for a given virtual machine
              Write-Host "Liczba udanych pełnych backupów dla VM '$vmName' w zadaniu '$jobName' w grudniu: $($successfulBackupsForVM.Count)"

              # Display the dates of successful full backups for a given VM
              $successfulBackupsForVM | ForEach-Object {
                  Write-Host "Data udanego pełnego backupu dla VM '$vmName': $($_.EndTime)"
              }
          }
      } 
      else 
      {
          Write-Host "No information about the VMs in the task."
      }
  }
Now it display:

Code: Select all

VMs in 'Backup Job MyJob' job:
Number of successful full backups for VM '[job] VMname' in 'Backup Job MyJob' task in November: 265
Date of successful full backups for VM '[job] VMname': 11/13/2023 12:29:21
Date of successful full backups for VM '[job] VMname': 11/13/2023 10:19:31
Date of successful full backups for VM '[job] VMname': 11/13/2023 08:09:43
Unfortunately, it counts for me how many of all jobs were successful, instead of the selected VM and it returns dates from all backups instead of the last success.
IainMcLaren
Lurker
Posts: 2
Liked: never
Joined: Dec 19, 2023 12:06 pm
Full Name: Iain McLaren
Contact:

Re: [Powershell] Effectiveness of backup for individual VMs

Post by IainMcLaren »

Hi Rafawi

In your VM foreach-object, you have stated the following line of code : $backupSessionsForVM = Get-VBRBackupSession

You didn't set a filter in the Get-VBRBackupSession to only apply to the VM in question. This will return all the backup sessions.
Post Reply

Who is online

Users browsing this forum: clintbergman and 15 guests