Question

VEM API QuickBackup

  • 17 August 2023
  • 1 comment
  • 65 views

Hi all!

I’ve tried to initiate a QuickBackup via VEM API. After starting my Script everything looks good and the response is 202. But when i take look into the VBR Console, there was no QuickBackup started.

Did i missed anything in my Script? Does anyone have a idea? 

#API_QuickBackup_singelVM
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 = "https://ourvemserver:9398/api/"
$authPath = 'C:\temp\myadmincred.xml'
$Credentials = IMPORT-CLIXML -path $authPath
$RESTAPIUser = $Credentials.UserName
$RESTAPIPassword = $Credentials.GetNetworkCredential().Password
$usrpwd = "$($RESTAPIUser):$($RestAPIPassword)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($usrpwd))
$basicAuthValue = "Basic $encodedCreds"

$Headers = @{}
$headers.Add("Authorization", "$basicAuthValue")

#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_7" })[0].Links

#login
$r_login = Invoke-WebRequest -method Post -Uri $r_api_links.Link.Href -Headers $Headers

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

#content
$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'}

# QuickBackup myVM
$j_body = @{
    VmRef="urn:VMware:Vm:a22adafe-9ca9-4dab-5555-56eef5e91231.vm-1555398"
      } | ConvertTo-Json -Compress

$r_qb_vc200 = "$($r_login_links_base.Href)backupServers/9dd3f7d5-5a37-5555-83d8-a20ddbe6d4c3"


$r_qb_job_start = "$($r_qb_vc200)?action=quickbackup"

Invoke-WebRequest -Method Post -Headers @{$sessionheadername = $sessionid } -Uri $r_qb_job_start -Body $j_body -ContentType 'application/json'
StatusCode        : 202
StatusDescription : Accepted
Content : {"TaskId":"task-4","State":"Running","Operation":"QuickBackup","Result":null,"Links":[{"Rel":"Delete","Href":"https://ourvemserver:9398/api/tasks/task-4","Name":null,"Type":n
ull}],"Href":"https...
RawContent : HTTP/1.1 202 Accepted
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
Content-Length: 260
Cache-Control: p...
Forms : {}
Headers : {[X-Frame-Options, SAMEORIGIN], [X-XSS-Protection, 1; mode=block], [Strict-Transport-Security, max-age=31536000], [X-Content-Type-Options, nosniff]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : System.__ComObject
RawContentLength : 260
$statustask = Invoke-WebRequest -Method Get -Headers @{$sessionheadername = $sessionid } -Uri "https://ourvemserver:9398/api/tasks/task-4"

StatusCode : 200
StatusDescription : OK
Content : <?xml version="1.0" encoding="utf-8"?><Task xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Href="https://ourvemserver:9398/api/tasks/task-4" ...
RawContent : HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
Content-Length: 547
Cache-Control: private...
Forms : {}
Headers : {[X-Frame-Options, SAMEORIGIN], [X-XSS-Protection, 1; mode=block], [Strict-Transport-Security, max-age=31536000], [X-Content-Type-Options, nosniff]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : System.__ComObject
RawContentLength : 547

 


1 comment

Userlevel 7
Badge +8

Hi Frank,

I added a few lines to your code to get the backup serverUID (hardcoded in your variable $r_qb_vc200) as well as the VmRef urn based on your selection. You just need to adjust the variable $VM2Backup in the script below (you could use a parameter to pass it to the script).

I was able to run the script successfully in my V12 environment.

Cheers,
Steve

 

#API_QuickBackup_singelVM
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 = "https://localhost:9398/api/"
$authPath = 'C:\temp\myadmincred.xml'
$Credentials = IMPORT-CLIXML -path $authPath
$RESTAPIUser = $Credentials.UserName
$RESTAPIPassword = $Credentials.GetNetworkCredential().Password
$usrpwd = "$($RESTAPIUser):$($RestAPIPassword)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($usrpwd))
$basicAuthValue = "Basic $encodedCreds"

$Headers = @{}
$headers.Add("Authorization", "$basicAuthValue")

#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_7" })[0].Links

#login
$r_login = Invoke-WebRequest -method Post -Uri $r_api_links.Link.Href -Headers $Headers

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

#content
$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 the Backupserver UID
$getbackupserverURI = $server+"backupServers"
$getbackupserverid = Invoke-WebRequest -Method GET -Headers @{$sessionheadername = $sessionid } -Uri $getbackupserveruri
$backupserveridxml = [xml]$getbackupserverid.Content
$backupserverUID = $backupserveridxml.EntityReferences.Ref.UID -split ':' | Select-Object -Last 1

# Lookup the VM for the Quickbackup
$lookupURI = $server+"LookupSvc"
$lookupVM = Invoke-WebRequest -Method GET -Headers @{$sessionheadername = $sessionid } -Uri $lookupURI
$lookupVMxml = [xml]$lookupVM
$lookupVMHref = $lookupVMxml.LookupSvc.Links.Link.Href -split "`r`n"
$lookupVMURI = $lookupVMHref | Where-Object { $_ -match "type=Vm" }
$lookupVMFinal = Invoke-WebRequest -Method GET -Headers @{$sessionheadername = $sessionid } -Uri $lookupVMURI
$lookupVMFinalXML = [xml]$lookupVMFinal.Content
$lookupVMFinalItems = $lookupVMFinalXML.HierarchyItems.HierarchyItem

# The VM for which you want to trigger a Quickback
$VM2Backup = "yourvmnamehere"

# Get the urn for the VM
$objectRef = $lookupVMFinalItems | Where-Object { $_.ObjectName -eq $VM2Backup }
$vmref = $objectRef.ObjectRef

# QuickBackup myVM
$j_body = @{
VmRef=$vmRef
} | ConvertTo-Json -Compress

$r_qb_vc200 = "$($r_login_links_base.Href)backupServers/$backupserverUID"
$r_qb_job_start = "$($r_qb_vc200)?action=quickbackup"

Invoke-WebRequest -Method Post -Headers @{$sessionheadername = $sessionid } -Uri $r_qb_job_start -Body $j_body -ContentType 'application/json'

 

Comment