Skip to main content

Veeam V11 (Automation) : Add Multiple Veeam Backup Servers to Veeam Enterprise Manager API.

Adding Multiple Veeam V11 Backup Servers to Veeam Enterprise Manager through Powershell & API.

Haven't written a post in sometime but i had a field request from a customer and thought it was suited for a Automation post.

The Request:

Client has multiple Veeam Backup Servers managing each individual remote site or Geo-located DC. To get a centralized view of each site its then advisable to add these Veeam Backup Servers to Veeam Enterprise Manager. Too do so you would need to follow a wizard for each Veeam Backup Server , when you have anything over 15-30 sites this can be a cumbersome task.

Solution Overview:

The solution was to automate this process and have commands create each Veeam Backup Server in Enterprise Manager. Usually i would use powershell but Enterprise Manager only supports API calls , so i used a bit of both. 

My logic to do this was : I needed Powershell to import a list of server names and IPs and then call an API command against Enterprise Manager for each server in the list.

Lets Begin:

First i created a CSV file with 2 columns ; Server Name ; IP ; you could just have a single column if you have perfect name resolution in your network.

Here is an example of My CSV file i created below :

I populated the CSV file with Hostnames & IPs so that i could use either in the script & saved this to the root of the C Drive / Volume on the Veeam Enterprise Manager Server.

C:\list.csv

Lets Start with the script:

First i need to import this CSV list and assign the Columns to Variables $

$list = Import-Csv -Path C:\list.csv
$servers = $list.name
$IPS = $list.Ip

Next in need to configure the Authentication for the Veeam Enterprise Manager API session and Future calls/posts.

$user = 'administrator'
$pass = 'Password'

$pair = "$($user):$($pass)"

$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

$basicAuthValue = "Basic $encodedCreds"

I specified a Username and Password that could authenticate against Veeam Enterprise Manager and then convert in such a way it can be used in a API call Header.

Next i define the header variable for the First authentication session against Veeam Enterprise Manager

$Headers = @{
    Authorization = $basicAuthValue }


In my Lab environment i have no certificates assigned to Veeam Enterprise Manager so i need have the API request ignore certificate errors , i do that here below.

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy


Next Step is to run the API Logon request , using the Header defined above and assigning its response to a Variable so i can uses that later in other requests.

$logon = Invoke-webrequest  -Method "POST" 
-Uri https://localhost:9398/api/sessionMngr/?v=latest -Headers $Headers

Next i further simplify the results of the $logon to only hold the Rest Session ID which i will use going forward to authenticate the rest of the requests.

$Response = $logon.Headers
$Sessionkey = $Response.'X-RestSvcSessionId'

I then create a second Header variable that will be used in the requests for adding Veeam Backup Servers to Veeam Enterprise Manager.

$Headers2 = @{
    'X-RestSvcSessionId' = $Sessionkey ;
}

Now comes the part of the script that would actually do the work for us , I used a powershell foreach loop that reads a set of objects (iterates) and completes when it's finished with the last one.

The objects are defined in  $IPs or $Servers imported from the CSV file.

Additionally i have set a common Body for each API request that will use the same information other than IP or host-name which it will retrieve from $IPs or $Servers

foreach ($IP in $IPS)
{
   
 $body = @{
    "Description" = "API added backup Server" ;
    "DnsNameOrIpAddress" = $IP ;
    "Port" = "9392" ;
    "Username" = "Domain\Administrator" ;
    "Password" = "Password"
}
 Invoke-WebRequest  -Method 'POST' 
-Uri https://localhost:9398/api/backupServers?action=create -Headers $Headers2 
 -Body ($body|ConvertTo-Json) -ContentType "application/json"
}

This will takes sometime to complete depending on the Length of the CSV list.

Once completed you should see Status codes 201 on all API requests in the list.

At this point you can log in to Enterprise Manager web portal and review the Veeam Backup Servers List. 

 

This can be a quick and easy way to add multiple Veeam Backup Servers to your Veeam Enterprise Manager for Global Overview.

The Full Script Below , Please share or leave a comment for script feedback or improvements. 


Comments

  1. Hello, thank you for the script. But it does not run. I think I do something wrong. All the variables in the script got the right values bot when I try on line 53 the Invoke-WebRequest in the Post-Method I get "Access denied.","StatusCode":403,"Status":"Forbidden". On the VBEM the Account got the Portal Administrator role an the Account is local Admin. I am using Veeam VBEM Version 12. Did you got any idea to solve this problem?

    Thank You Peter (from Munich now living in Buenos Aires)
    PS: I was a few times in Joburg and Capetown

    ReplyDelete

Post a Comment

Leave your Thoughts & Comments , and I'll reply as soon as possible.
Thank you for you views in Advance.