Virtually Sober

If there is free booze and Virtualization; I'm there!

Introduction to PowerShell and Zerto REST API scripting

It has been quite some time since my last post and to make up for this I’m going to give you some seriously useful goodies, based on using PowerShell in combination with the Zerto REST API!

Utilizing PowerShell with the REST API opens up a world of possibilities as you are no longer restricted to solely using the PowerShell cmdlets provided by Zerto. You can now report and automate tasks that previously were only possible using the GUI. Below are some of the examples I’m going to be covering in the next few blog posts:

  1. Long term RPO & storage reporting
  2. Automating VRA deployment
  3. Automating VM protection with vRealize Orchestrator

Before we get into detail of specific examples you first need to build a PowerShell script to authenticate and create a session to the Zerto Virtual Manager and the vCenter it is connected to. The first step is to create variables based on all the parameters needed to authenticate:

$ZertoServer = "192.168.0.31"
$ZertoPort = "9669"
$ZertoUser = administrator@lab.local
$ZertoPassword = "Password123!"
$vCenterServer = "192.168.0.81"
$vCenterUser = administrator@lab.local
$vCenterPassword = "Password123!"

Then we need to load the VMware PowerCLI cmdlets and establish a connection with the vCenter:

function LoadSnapin{
param($PSSnapinName)
if (!(Get-PSSnapin | where {$_.Name   -eq $PSSnapinName})){
Add-pssnapin -name $PSSnapinName
}
}
# Loading snapins and modules
LoadSnapin -PSSnapinName   "VMware.VimAutomation.Core"
# Connecting to the vCenter
connect-viserver -Server $vCenterServer -User $vCenterUser -Password $vCenterPassword

You don’t need to connect to the vCenter to work with the Zerto API and the alternative is to add the ZVM certificate to your trusted local certs. However, I install so many builds of Zerto it is much easier and more robust to simply connect to the vCenter in the session to remove this requirement.

We then need to build the URLs needed and authenticate against the Zerto API:

# 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}

Once authenticated we can start to use and interact with the API. For details on exactly what is possible you should consult the REST API Reference Guide.pdf included in the Zerto documentation, but here are some easy to use examples showing you all of the information available from each API call:

# Get Local Site Status
$LocalSiteInfoURL = $BaseURL+"localsite"
$LocalSiteInfoCMD = Invoke-RestMethod -Uri $LocalSiteInfoURL -TimeoutSec 100 -Headers $zertSessionHeader -ContentType "application/JSON"
<$LocalSiteInfoCMD | Select *

# Get Peer Site Status
$PeerSiteInfoURL = $BaseURL+"peersites"
$PeerSiteInfoCMD = Invoke-RestMethod -Uri $PeerSiteInfoURL -TimeoutSec 100 -Headers $zertSessionHeader -ContentType "application/JSON"
$PeerSiteInfo = $PeerSiteInfoCMD | Select *
$PeerSiteInfo

# Get Protected VMs List
$ProtectedVMsURL = $BaseURL+"vms"
$ProtectedVMsCMD = Invoke-RestMethod -Uri $ProtectedVMsURL -TimeoutSec 100 -Headers $zertSessionHeader -ContentType "application/JSON"
$ProtectedVMs = $ProtectedVMsCMD | Select *
$ProtectedVMs

# Get Virtual Protection Group List
$VPGsURL = $BaseURL+"vpgs"
$VPGsCMD = Invoke-RestMethod -Uri $VPGsURL -TimeoutSec 100 -Headers $zertSessionHeader -ContentType "application/JSON"
$VPGs = $VPGsCMD | Select *
$VPGs

# Get VRA List
$VRAsURL = $BaseURL+"vras"
$VRAsCMD = Invoke-RestMethod -Uri $VRAsURL -TimeoutSec 100 -Headers $zertSessionHeader -ContentType "application/JSON"
$VRAs = $VRAsCMD | Select *
$VRAs

Here is an example output using the Peer Site Status API:

PeerSiteAPIOutputExample

If you are copying and pasting these commands into PowerShell right now you will see a crazy amount of useful information from Zerto. How cool is that?! Don’t forget to disconnect your vCenter connection at the end of all your scripts with:

disconnect-viserver $vCenterServer -Force -Confirm:$false

In subsequent blog posts I will give you some examples of what you can do with this information and how you can use the API to not only retrieve information, but also send commands to automate processes too!

I hope you found this blog post useful and questions or comments let me know. Happy scripting,

Joshua

2 responses to “Introduction to PowerShell and Zerto REST API scripting

  1. niktips February 12, 2017 at 2:18 am

    Hi Josh, got inspired by your series of Zerto automation posts to script some of the work I do. I figured I’d start with something simple, like listing VPG configuration.

    PowerShell cmdlets seems to be capable of only listing VPG names, so I went down the REST API path. I can successfully retrieve the VPG configuration object, but most of the settings I’m looking for seems to be empty, such as recovery/test failover networks, recovery/test failover IPs etc.

    More specifically I get nil for network identifiers “” and empty VM NIC nodes “”. Could that be related to the fact that I have a Zerto to AWS scenario?

    • Joshua Stenhouse March 8, 2017 at 2:31 pm

      Hey Nik! So you are trying to get all the settings of a VPG using the manage VPG API and not getting anything for your VMs replicating to AWS? This is what it sounds like to me and if so, you actually found the limit of manage VPG API itself. Right now (as of ZVR 5.0 U2) it only supports vSphere to vSphere VMs. It doesn’t support Hyper-V, AWS or Azure unfortunately. Hopefully, they will fix it soon and I’d encourage you to raise it with your Zerto SE to help build the priority. I can moan about it all day long, which I do, but we need groundswell.

Leave a Reply

%d bloggers like this: