RESTful knowledge exchange
Post Reply
Posts: 1
Liked: never
Joined: Apr 27, 2022 5:56 am
Full Name: Thierry

powershell and VEM's API

Post by Thierry »

Hi Community,

I'm trying to make API query on my VEM using powershell 5
I'd like to setup API_Backup_Session_Report.ps1 available in ... Report.ps1

I'm block on first line use to do auth :
$r_api = Invoke-WebRequest -Method Get -Uri "https://$($server):9398/api/"
PS output is not very helpfull as no very verbose :

Code: Select all

nvoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:20 char:10
+ $r_api = Invoke-WebRequest -Method Get -Uri "https://$($server):9398/ ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
VEM web api is working fine, and as far not having much detail in PS output, i've no idea were to dig in to troubleshoot.
Any hint ?


Veeam Software
Posts: 3150
Liked: 1080 times
Joined: May 13, 2017 4:51 pm
Full Name: Fabian K.
Location: St. Gallen, Switzerland

Re: powershell and VEM's API

Post by Mildur »

Hi Thierry

I moved your topic to the RestAPI subforum.
This PowerShell scripts are not managed or tested by veeam.
And the Author of this script works for a different company now.
You can try our support, but I don't think they will help with this custom script.

Do you have tried to write your own script to get some information out of VEM? Does that work?
Probably one of my colleagues can help out with your question.
Product Management Analyst @ Veeam Software

Veeam Software
Posts: 717
Liked: 376 times
Joined: Jul 17, 2015 6:54 pm
Full Name: Jorge de la Cruz

Re: powershell and VEM's API

Post by jorgedlcruz » 1 person likes this post

So straight from the GitHub, I had the very same error as yourself, I have also encountered other problems, as I am using Windows Server 2022, which does not have Internet Explorer at all.

The main changes are:
  • Changed to use VEM API v1_6 which is the most recent for v11
  • Changed the Server, so it has all variables on a single string, as the $($server) was not working as expected, using the latest PowerShell versions
  • Added the -UseBasicParsing after every Invoke-WebRequest, as said, due to using Windows Server 2022
Could you perhaps give it a try?

In essence, I have made some edits and now it worked perfectly fine, here is the raw code, will try to put it on github later on:

Code: Select all

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

$server = ""
$username = 'administrator'
$report_filepath = 'C:\temp\api_backup_session_report.csv'

#get the api
$r_api = Invoke-WebRequest -Method Get -Uri $server -UseBasicParsing
$r_api_xml = [xml]$r_api.Content
$r_api_links = @($r_api_xml.EnterpriseManager.SupportedVersions.SupportedVersion | Where-Object { $_.Name -eq "v1_6" })[0].Links

$r_login = Invoke-WebRequest -method Post -Uri $r_api_links.Link.Href -UseBasicParsing -Credential (Get-Credential -Message "Basic Auth" -UserName "$username")

$sessionheadername = "X-RestSvcSessionId"
$sessionid = $r_login.Headers[$sessionheadername]

$r_login_xml = [xml]$r_login.Content
$r_login_links = $r_login_xml.LogonSession.Links.Link
$r_login_links_base = $r_login_links | Where-Object {$_.Type -eq 'EnterpriseManager'}

#get list of all backup jobs
$r_jobs_query = $r_login_links_base.Href + 'query?type=Job&filter=JobType==Backup'
$r_jobs = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid} -Uri $r_jobs_query -UseBasicParsing
$r_jobs_xml = [xml]$r_jobs.Content
$r_jobs_list = $r_jobs_xml.QueryResult.Refs.Ref.Href

#parse job list to get backup sessions
foreach ($r_jobs_link in $r_jobs_list) {

    #gather included tags detail
    $r_job_detail_link = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid} -Uri $(($r_jobs_link) + "?format=Entity") -UseBasicParsing
    $r_job_detail_link_xml = [xml]$r_job_detail_link.Content
    $r_job_detail_included_tags = $r_job_detail_link_xml.Job.JobInfo.BackupJobInfo.Includes.ObjectInJob | Where-Object HierarchyObjRef -like "*InventoryServiceTag*"

    #gather backup session entities
    $r_backup_session_entity_list = @()
    $r_backup_session_link = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid} -Uri $(($r_jobs_link) + "/backupSessions") -UseBasicParsing
    $r_backup_session_link_xml = [xml]$r_backup_session_link.Content
    $r_backup_session_entity_list = $r_backup_session_link_xml.EntityReferences.Ref.Links.Link | Where-Object Type -eq 'BackupJobSession' | Select-Object -ExpandProperty Href

    #gather task sessions
    foreach ($r_backup_session_entity in $r_backup_session_entity_list) {
        $r_backup_session_entity_link = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid} -Uri $r_backup_session_entity -UseBasicParsing
        $r_backup_session_entity_link_xml = [xml]$r_backup_session_entity_link
        $r_task_session_ref = $r_backup_session_entity_link_xml.BackupJobSession.Links.Link | Where-Object Type -eq BackupTaskSessionReferenceList | Select-Object -ExpandProperty Href
        $r_task_session_link = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid} -Uri $r_task_session_ref -UseBasicParsing
        $r_task_session_link_xml = [xml]$r_task_session_link
        $r_task_session_list = $r_task_session_link_xml.EntityReferences.Ref.Href
        #gather task session details
        foreach ($r_task_session in $r_task_session_list) {
            $r_task_session_entity = $($r_task_session + "?format=Entity")
            $r_task_session_entity_link = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid} -Uri $r_task_session_entity -UseBasicParsing
            $r_task_session_entity_link_xml = [xml]$r_task_session_entity_link
            $r_task_session_detail = $r_task_session_entity_link_xml.BackupTaskSession

            #gather VM restore points
            $r_vm_restore_point_link = ($r_task_session_entity_link_xml.BackupTaskSession.Links.Link | Where-Object Type -eq VmRestorePoint)

            #gather VM restore point entities & details
            if ([bool]$r_vm_restore_point_link) {
                $r_vm_restore_point_entity_link = $r_vm_restore_point_link | Select-Object -ExpandProperty Href
                $r_vm_restore_point_entity = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid} -Uri $r_vm_restore_point_entity_link -UseBasicParsing
                $r_vm_restore_point_entity_xml = [xml]$r_vm_restore_point_entity
                $r_vm_restore_point_entity_detail = $r_vm_restore_point_entity_xml.VmRestorePoint

                $r_vm_backup_file_link = $(($r_vm_restore_point_entity_xml.VmRestorePoint.Links.Link | Where-Object Href -like "*backupFiles*" | Select-Object -ExpandProperty Href) + "?format=Entity")
                $r_vm_backup_file_entity = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid} -Uri $r_vm_backup_file_link -UseBasicParsing
                $r_vm_backup_file_entity_xml = [xml]$r_vm_backup_file_entity
                $r_vm_backup_file_entity_detail = $r_vm_backup_file_entity_xml.BackupFile

                #VM restore point, task session, & job details
                $backup_session_detail = New-Object PSObject -Property @{
                    'VMName'                  = $r_vm_restore_point_entity_detail.VMName
                    'BackupCreationTime(UTC)' = $r_task_session_detail.CreationTimeUTC
                    'BackupEndTime(UTC)'      = $r_task_session_detail.EndTimeUTC
                    'State'                   = $r_task_session_detail.State
                    'Result'                  = $r_task_session_detail.Result
                    'Reason'                  = $r_task_session_detail.Reason
                    'TotalSize'               = $r_vm_backup_file_entity_detail.BackupSize
                    'JobAlgorithm'            = $r_vm_restore_point_entity_detail.Algorithm
                    'RestorePointType'        = $r_vm_restore_point_entity_detail.PointType
                    'JobName'                 = $r_job_detail_link_xml.Job.Name
                    'IncludedTags'            = $r_job_detail_included_tags.Name
                $backup_session_detail | Export-Csv -Path $report_filepath -NoTypeInformation -Append


thank you
Jorge de la Cruz
Senior Analyst, Product Management | Veeam ONE @ Veeam Software

@jorgedlcruz /
vExpert 2014-2021 / Nutanix Technology Champion 2018-2019 / InfluxAce

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest