RESTful knowledge exchange
Post Reply
eeldivad
Influencer
Posts: 11
Liked: never
Joined: Apr 23, 2019 3:43 pm
Full Name: David Lee
Contact:

How to generate list of machines view like Enterprise Manager -> Machines tab

Post by eeldivad »

I would like to get the list of machines and all the columns just like it shows on the Enterprise Manager -> Machines tab view with the following columns
- Machine
- Backup Server
- Job Name
- Restore Points
- Location
- Last Success
- Path

Is it possible using the REST API? If so, has someone done this yet and can share or point me in right direction? I'm new to the API and this would be my first time working with it so any references and sample code would help.

Thanks
eeldivad
Influencer
Posts: 11
Liked: never
Joined: Apr 23, 2019 3:43 pm
Full Name: David Lee
Contact:

Re: How to generate list of machines view like Enterprise Manager -> Machines tab

Post by eeldivad »

I looked through the API references and tested out some queries but nothing seems to give a list of machines that are shown in the machines tab of Enterprise Manager. I can't believe a list of machines is not available through the API since it's important to know what machines are backed up. Or maybe I'm missing something if someone has any ideas? I just need an automated way to get this data either through REST API, powershell, or direct to SQL queries if someone knows how.

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

Re: How to generate list of machines view like Enterprise Manager -> Machines tab

Post by oleg.feoktistov » 1 person likes this post

Hi David,

I don't see how it is possible to get the exact view just with server-side EM REST API features, but I'm currently making a script with client-side filtering. I'll update the thread as soon as I finish one.

Thanks,
Oleg
eeldivad
Influencer
Posts: 11
Liked: never
Joined: Apr 23, 2019 3:43 pm
Full Name: David Lee
Contact:

Re: How to generate list of machines view like Enterprise Manager -> Machines tab

Post by eeldivad »

Thank you, please let me know once you have it
oleg.feoktistov
Veeam Software
Posts: 1918
Liked: 636 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: How to generate list of machines view like Enterprise Manager -> Machines tab

Post by oleg.feoktistov » 2 people like this post

Hi @eeldivad,

I've been looking forward to writing the machinesView interface for REST API, so here is the way I implemented it through Powershell client.
Two files - authorization and machinesView itself.

Authorization.ps1:

Code: Select all

# Create base url variable and headers dictionary
$baseUrl = 'https://<hostAddress>:9398/api'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

# Get credentials and compose a credentials string
$creds = # here you can either get creds from some credentials vault or prompt for input right in the code with Get-Credential
$credsString = "$($username):$($password)" <# You will need to get plain username and password here from the creds you obtained in the previous step. 
These variable are just examples. #>

$bytes = [System.Text.Encoding]::UTF8.GetBytes("$($credsString)")
$authorizationCode = [System.Convert]::ToBase64String($bytes)
$headers.Add("Accept", "application/json")
$headers.Add("Authorization", "Basic $($authorizationCode)") <# Another way would be just to convert your credentials to base64String in advance
and insert it as an authorization header here. #>

# Creation session through REST and save session id as a global variable
$response = Invoke-WebRequest "$($baseUrl)/sessionMngr/?v=latest" -Method 'POST' -Headers $headers
$global:sessionId = $response.Headers.'X-RestSvcSessionId' 

MachinesView.ps1:

Code: Select all

# Create base url variable and headers with a session id you save to $Global:sessionId
$baseUrl = 'https://<HostUrl>:9398/api'
$headers = @{
  'X-RestSvcSessionId' = $Global:sessionId
  'Accept' = 'application/json'
}

# Function to get all backups with specific properties
function Get-Backups {
  $backupsUrl = "$($baseUrl)/backups"
  $response = Invoke-WebRequest -Method Get -Uri $backupsUrl -Headers $headers
  $converted = $response.Content | ConvertFrom-Json
  foreach ($backup in $converted.Refs) {
    $response = Invoke-WebRequest -Method Get "$($backup.Href)?format=Entity" -Headers $headers
    $converted = $response.Content | ConvertFrom-Json
    $backup | select Name, Href, @{n='BackupServer';e={$_.Links[0].Name}}, @{n='Location';e={$_.Links[1].Name}}, `
    @{n='Platform';e={$converted.Platform}}
  }
}

# Function to get all vmRestorePoints as per backup url specified
function Get-RestorePoints {
  [CmdletBinding()]
  param (
  [String]$BackupUrl
  )
  $rpsUrl = "$($BackupUrl)/restorePoints"
  $response = Invoke-WebRequest -Method Get -Uri $rpsUrl -Headers $headers
  $converted = $response.Content | ConvertFrom-Json
  foreach ($rp in $converted.Refs) {
    $vmRpUrl = "$($rp.Href)/vmRestorePoints"
    $response = Invoke-WebRequest -Method Get $vmRpUrl -Headers $headers
    $converted = $response.Content | ConvertFrom-Json
    foreach ($vmRp in $converted.Refs) {
      $vmRp | select Name, @{n='BackupServer';e={$_.Links[0].Name}}, @{n='RestorePointUrl';e={$rp.Href}}
    }
    

  }
  
}

# Function to get vApp if backup of vCloud platform type
function Get-VApp {
  [CmdletBinding()]
  param(
   [String]$RestorePointUrl
  )
  $vAppUrl = "$($RestorePointUrl)/vAppRestorePoints"
  $response = Invoke-WebRequest -Method Get -Uri $vAppUrl -Headers $headers
  $converted = $response.Content | ConvertFrom-Json
  $vApp = ($converted.Refs[0].Name -split '@')[0]
  $vApp
}

# Get all backups
$backups = Get-Backups
$sum = @()

# Process each backup, figuring out machines names, restore point count, location etc.
foreach ($backup in $backups) {
    $vmRestorePoints = @()
    
    $restorePoints = Get-RestorePoints -BackupUrl $backup.Href
    if ($backup.Platform -eq 'vCloud') {
      $vApp = Get-VApp -RestorePointUrl $restorePoints[0].RestorePointUrl
    }
    else {
      $vApp = 'NotAvailable'
    }
    foreach ($rp in $restorePoints) {
    $vmRestorePoints += $rp
    }
    
    $includes = @()
    foreach ($rp in $vmRestorePoints) {
     $includeName = ($rp.Name -split '@')[0]
     $includes += $rp | select @{n='Name';e={$includeName}}
    }
    $includes = $includes | select -Property Name -Unique
    foreach ($include in $includes) {
      $rpCount = 0
      $dates = @()
      foreach ($rp in $vmRestorePoints) {
      $rpName = ($rp.Name -split '@')[0]
       if ($rpName -eq $include.Name -and $rp.BackupServer -eq $backup.BackupServer) {
         $rpCount += 1
         $date = ($rp.Name -split '@')[1]
         $dates += $date
       }
      }
      $lastSuccess = $dates | Sort-Object -Descending | select -First 1
     $sum += $include | select Name, @{n='BackupServer';e={$backup.BackupServer}}, @{n='JobName';e={$backup.Name}}, `
      @{n='RestorePoints';e={$rpCount}}, @{n='Location';e={$backup.Location}}, @{n='LastSuccess';e={$lastSuccess}}, @{n='Platform';e={$backup.Platform}}, `
      @{n='vApp';e={$vApp}}
    }
}
$sum
The only property I couldn't get through REST was Path as it is not exposed there. For convenience, I also included Platform column in the final view. The view itself is assigned to $sum variable, so you can then either save it to a file, send via e-mail, create html report or else.

I believe, that's just an example of how this view can be implemented through Powershell client libraries, but it's what I intend to use for my lab, so decided to share anyways. :wink:

Don't forget to populate $baseUrl variable with the right Enterprise Manager host address.

Hope it helps anybody,
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests