Press "Enter" to skip to content

Getting a List of VMs by VPG

Joshua Stenhouse 0

Hi all!

In response to a recent MyZerto forum request I thought I’d write a short post on how to obtain a simple piece of information from Zerto; what VMs are protected and their VPG name.

Rather than reference back to multiple posts I’ll take you through all the elements needed and then put it together at the end of the blog post. Let’s start by setting the variables needed which should be configured for your environment:

# Script variables
$ZertoServer = "192.168.0.31"
$ZertoPort = "9669"
$ZertoUser = "administrator@lab.local"
$ZertoPassword = "Password123!"

We now need to set the certificate behavior (to avoid connectivity issues) and the Zerto API session information, there is nothing to change in any of the code examples going forward:

# Setting Cert Policy
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
# Building Zerto API string and invoking API
$baseURL = "https://" + $ZertoServer + ":"+$ZertoPort+"/v1/"
# Authenticating with Zerto APIs
$xZertoSessionURI = $baseURL + "session/add"
$authInfo = ("{0}:{1}" -f $ZertoUser,$ZertoPassword)
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
$authInfo = [System.Convert]::ToBase64String($authInfo)
$headers = @{Authorization=("Basic {0}" -f $authInfo)}
$sessionBody = '{"AuthenticationMethod": "1"}'
$contentType = "application/json"
$xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURI -Headers $headers -Method POST -Body $sessionBody -ContentType $contentType
#Extracting x-zerto-session from the response, and adding it to the actual API
$xZertoSession = $xZertoSessionResponse.headers.get_item("x-zerto-session")
$zertSessionHeader = @{"x-zerto-session"=$xZertoSession}

Now we have built the session information and connected to the API it gets devastatingly easy to get the information we need! Lets get a list of all the VMs, select some of the columns of interest then show the results in a table:

# Querying API
$VMListURL = $BaseURL+"vms"
$VMList = Invoke-RestMethod -Uri $VMListURL -TimeoutSec 100 -Headers $zertSessionHeader -ContentType "application/JSON"
$VMListTable = $VMList | select VmName, VpgName, UsedStorageInMB, SourceSite, TargetSite, Priority
$VMListTable | format-table -AutoSize

Here is an example of what you should see:

VMListOutput

Let’s say we now want to filter the results by a specific VPG name, as per the original request, all we have to do is:

# Selecting VPG name in table
$VMListTableFiltered | Where-Object VpgName -EQ "VMware-VPN" | format-table -AutoSize

And that’s it! Keep posting requests for any other examples you need and happy scripting. The full script can be copied from the below. Thanks,

Joshua

# Script variables
$ZertoServer = "192.168.0.31"
$ZertoPort = "9669"
$ZertoUser = "administrator@lab.local"
$ZertoPassword = "Password123!"
# Setting Cert Policy
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
# Building Zerto API string and invoking API
$baseURL = "https://" + $ZertoServer + ":"+$ZertoPort+"/v1/"
# Authenticating with Zerto APIs
$xZertoSessionURI = $baseURL + "session/add"
$authInfo = ("{0}:{1}" -f $ZertoUser,$ZertoPassword)
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
$authInfo = [System.Convert]::ToBase64String($authInfo)
$headers = @{Authorization=("Basic {0}" -f $authInfo)}
$sessionBody = '{"AuthenticationMethod": "1"}'
$contentType = "application/json"
$xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURI -Headers $headers -Method POST -Body $sessionBody -ContentType $contentType
#Extracting x-zerto-session from the response, and adding it to the actual API
$xZertoSession = $xZertoSessionResponse.headers.get_item("x-zerto-session")
$zertSessionHeader = @{"x-zerto-session"=$xZertoSession}
# Querying API
$VMListURL = $BaseURL+"vms"
$VMList = Invoke-RestMethod -Uri $VMListURL -TimeoutSec 100 -Headers $zertSessionHeader -ContentType "application/JSON"
$VMListTable = $VMList | select VmName, VpgName, UsedStorageInMB, SourceSite, TargetSite, Priority
$VMListTable | format-table -AutoSize
# Selecting VPG name in table
$VMListTableFiltered | Where-Object VpgName -EQ "VMware-VPN" | format-table -AutoSize

Leave a Reply

%d bloggers like this: