Automating ZVR with PowerShell and REST APIs Whitepaper

[Pages:22]Automating Zerto with PowerShell and REST APIs

Version 4.0 August 2022

Legal Disclaimer

Copyright ? 2022, Zerto Ltd. All rights reserved. Information in this document is confidential and subject to change without notice and does not represent a commitment on the part of Zerto Ltd. Zerto Ltd. does not assume responsibility for any printing errors that may appear in this document. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or information storage and retrieval systems, for any purpose other than the purchaser's personal use, without the prior written permission of Zerto Ltd. All other marks and names mentioned herein may be trademarks of their respective companies. The scripts are provided by example only and are not supported under any Zerto support program or service. All examples and scripts are provided "as-is" without warranty of any kind. This includes encrypting passwords. In all the examples provided in this document the passwords are stored using plain text within your PowerShell scripts. This is often against company policy and so is only done for ease of testing and there are multiple ways to remove the use of plain text passwords. The author and Zerto further disclaim all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. In no event shall Zerto, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if the author or Zerto been advised of the possibility of such damages. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.

Automating Zerto with PowerShell and REST APIs

1 of 19

Table of Contents

LEGAL DISCLAIMER................................................................................................................................. 1 INTRODUCTION ...................................................................................................................................... 3

What is PowerShell? ...................................................................................................... 3 What is a cmdlet?........................................................................................................... 3 Understanding Variables & Arrays ................................................................................ 3 What are REST APIs? ...................................................................................................... 4 SCRIPTING BEST PRACTICES ............................................................................................................... 4 PREREQUISITES ...................................................................................................................................... 4 RESOURCES ............................................................................................................................................. 5 API Documentation ....................................................................................................... 5 Zerto PowerShell Cmdlet Documentation ................................................................... 5 GETTING STARTED ................................................................................................................................. 5 Connecting to Zerto APIs Traditionally ........................................................................ 5 Downloading Zerto cmdlets.......................................................................................... 5 ZERTO POWERSHELL SCRIPTS ........................................................................................................... 6 CONNECT TO THE ZERTO VIRTUAL MANAGER (ZVM) .................................................................... 6 Deploying Virtual Replication Appliances (VRAs) ......................................................... 7 DEPLOYING VIRTUAL PROTECTION GROUPS (VPGS)................................................................... 10 FAILOVER TEST SCRIPT ....................................................................................................................... 16 ARCHIVED REFERENCES ..................................................................................................................... 18

Automating Zerto with PowerShell and REST APIs

2 of 19

Introduction

Zerto, a Hewlett Packard Enterprise company, empowers customers to run an always-on business by simplifying the protection, recovery, and mobility of on-premises and cloud applications. Zerto's cloud data management and protection solution eliminate the risks and complexity of modernization and cloud adoption across private, public, and hybrid deployments. The simple, software-only solution uses continuous data protection at scale to converge disaster recovery, backup, and data mobility. Zerto is trusted by over 9,500 customers globally and is powering offerings for Microsoft Azure, IBM Cloud, AWS (Amazon Web Services), Google Cloud, Oracle Cloud, and more than 350 managed service providers.

This guide provides an overview of how to utilize PowerShell to start scripting powerful automated disaster recovery procedures using Zerto REST APIs. Doing so enables the reduction of manual processes, scripting installations, integrating with third-party tools, and the realization of the full benefits of software-defined replication and recovery in protecting a virtualized IT infrastructure. This guide is for PowerShell users who may want to implement any of the use cases mentioned:

? Automating virtual machine (VM) protection

? Automating VM protection with vRealize Orchestrator

? Establishing a connection to the Zerto Virtual Machine (ZVM)

? Bulk VRA deployment

? Bulk VPG configuration

? Failover Test

The examples given have been developed to meet the requirements of Zerto cloud and enterprise environments. The font size of the examples given are optimized depending on the maximum line length to enable copying and pasting directly from the PDF into your PowerShell scripts.

What is PowerShell?

PowerShell is a command-line shell and a scripting language all in one. It allows users to run commands on local or remote computers to automate tasks like managing users and automating workflows. By default, PowerShell offers many helpful commands and can increase its capabilities by installing more modules.

What is a cmdlet?

A cmdlet (pronounced "command-let") is a compiled command that runs in PowerShell. These compiled lines of code execute pre-defined functions. For example, the Get-Help core cmdlet to invoke a built-in help system for a developer to troubleshoot the code more efficiently.

Understanding Variables & Arrays

To store data for execution within PowerShell scripts, it is important to use variables and arrays for efficiency. Variables allow a value to be used multiple times and it only needs to be changed once, making editing scripts easier. For ease of use, the same variables will be used in all examples in this document, these are:

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

Automating Zerto with PowerShell and REST APIs

3 of 19

Arrays are used in many examples in this document. These are used to build tables of data obtained from the APIs and multiple data sources simultaneously. The array is then used to export data to CSVs or to perform multiple actions within the script. Arrays are created line by line with the required values which are then added together. Below as an example of how to create a simple array:

$Value1 = "FirstValue" $Value2 = "SecondValue" $Array = @() $ArrayLine = new-object PSObject $ArrayLine | Add-Member -MemberType NoteProperty -Name "Value1" -Value $Value1 $ArrayLine | Add-Member -MemberType NoteProperty -Name "Value2" -Value $Value2 $Array += $ArrayLine $Array

What are REST APIs? An Application Programming Interface (API) is a set of clearly defined methods of communication between various software components. Representational State Transfer (REST) determines what the API looks like. It is also a set of rules that developers follow when creating an API. Together, a REST API allows you to query an API via a URL and request a response or send a change to the API via a URL and modify the data.

The REST APIs utilized in the examples are all included within the Zerto Virtual Manager (ZVM) documentation set. The document does not intend to give examples for all the available APIs. Its purpose is to give examples of the most requested use cases and share both information and knowledge on how to customize and create automation scripts.

Scripting Best Practices

Scripting and automating tasks in a production environment can introduce an element of risk if care is not taken to ensure the scripts have been tested. To mitigate this risk, follow the below best practices:

1. Use test VMs where applicable for initial testing 2. Use a demo lab or separate vSphere environment if available for testing 3. Test scripts using PowerShell ISE to aid with troubleshooting before scheduling a script 4. Use transcripts to maintain audit trails and aid with troubleshooting 5. Create descriptive comments throughout scripts to make it easier to understand and edit 6. Use version control, e.g. with git, on scripts with last known good configurations 7. Manage and schedule scripts from a central server 8. Document scripts and automated processes 9. Ensure IT teams know how to enable and disable scripts

Prerequisites

All the example scripts given in this document share a common set of minimum requirements. These are:

? Zerto Virtual Manager (ZVM) is deployed

o ZVM Server Name, username, and password with permission is used to access its API

? Network connectivity to the ZVM

? Access permission to write in and create the directory specified for logging, or manually create the directory in advance

Automating Zerto with PowerShell and REST APIs

4 of 19

? Make sure PowerShell version 6.1 or greater is installed and being utilized

o Download the MSI of version 7 here

Some example scripts also directly interact with the vCenter or a Zerto Cloud Manager (ZCM). For these examples, the following requirements must also be met:

? VMware PowerCLI is installed on the host running the script ? vCenter Server Name, username, and password to establish as session using PowerCLI to the vCenter ? Network connectivity to the vCenter ? ZCM with a new service profile configured and applied to both site ZVMs (source and target) ? Access to a CSV editor (i.e., Microsoft Excel, CSVed)

Resources

API Documentation 1. Zerto Virtual Manager 2. Zerto Analytics 3. Zerto Cloud Manager 4. Zerto for Kubernetes 5. Zerto In-Cloud for AWS

This paper focuses on using PowerShell with the Zerto Virtual Manager (ZVM) API, but the concepts are applicable more broadly.

With the exception of the Zerto Cloud Manager (ZCM) APIs, all other Zerto APIs use Swagger. Swagger accelerates the time to build and learn REST APIs by using the API itself to self-document and then comprehensively describe its structure in a clear, interactive format.

Zerto PowerShell Cmdlet Documentation ? About Zerto Cmdlets ? Full List of Commands

Getting Started

Downloading Zerto cmdlets For PowerShell scripts to interact with Zerto's REST APIs, a PowerShell command-line interface (CLI) should be used on the Windows instance hosting the ZVM. The PowerShell script is also required to be created on the machine where the Zerto Virtual Manager (ZVM) that manages the recovery is installed. Ensure that PowerShell is "Run as Administrator" to avoid any potential permissioning issues.

Starting with Zerto 9.0, cmdlets are available only from the PowerShell Gallery. Download the official Zerto commandlets by entering the following in PowerShell:

PS C:\Users\Zerto> Install-Module Zerto.mandlets

Automating Zerto with PowerShell and REST APIs

5 of 19

Next, import the Zerto commandlets:

PS C:\Users\Zerto> Import-Module Zerto.mandlets

Type "A" and then press "Enter" to make the selection of "[A] Yes to All".

Zerto PowerShell Scripts

All code is shared at the Zerto GitHub page which can be used to follow along.

Connect to the Zerto Virtual Manager (ZVM)

The below are code snippets which establish a connection to the ZVM API.

At the start, we define four variables required to initiate the connection. First is the IP address of the deployed ZVM. Second is the open port number on the ZVM that a connection will be established through; by default, this is 9669. Finally, both the ZVM username and password to enter the interface is required.

################################################ # Input the variables below ################################################ $ZertoServer = "" #ZVM IP Address $ZertoPort = "" #open communication port on ZVM; default: 9669 $ZertoUser = "" #ZVM username $ZertoPassword = "" #ZVM password

The next section of code is required for a successful authentication with the Zerto API without connecting to vSphere using PowerCLI. This certificate authorizes the API calls.

################################################ # Setting Certificate Policy ################################################ add-type @"

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

Automating Zerto with PowerShell and REST APIs

6 of 19

Finally, the end of the script builds the Zerto API string and invokes the Zerto API. Note the variables in base URL should specifically include the ZVM IP address and open port. It ends with an output of the API response.

################################################ # Building Zerto API string and invoking API ################################################ $BaseURL = "https://" + $ZertoServer + ":"+$ZertoPort+"/v1/" # Authenticating with Zerto APIs $xZertoSessionURL = $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"}' $TypeJSON = "application/JSON" $TypeXML = "application/XML" Try { $xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURL -Headers $Headers Method POST -Body $SessionBody -ContentType $TypeJSON } Catch { Write-Host $_.Exception.ToString() $error[0] | Format-List -Force } #Extracting x-zerto-session from the response, and adding it to the actual API $xZertoSession = $xZertoSessionResponse.headers.get_item("x-zerto-session") $ZertoSessionHeader = @{"x-zerto-session"=$xZertoSession; "Accept"=$TypeJSON}

Write-Output $ZertoSessionHeader

Deploying Virtual Replication Appliances (VRAs)

While the Zerto user interface is helpful for a few VRA installations, and includes automated cluster-wide deployment options, scripts can still really help when you must deploy many VRAs in both production and secondary/peer sites and want more control than what the GUI can offer. Zerto recommends installing VRAs on all hosts in a cluster on which protected virtual machines (VMs) also reside.

To simplify the deployment, a CSV file is referred to by the PowerShell script which provides specific data points about the ESXi host which is in the GitHub repository. Save the file with an appropriate name in a directory which can be easily accessed. Optionally, added in the first portion of the code is a "Seconds between VRA Deployments" variable which will create a time buffer between VRA deployments if deploying multiple.

################################################ # Configure the variables below ################################################ $LogDataDir = "" #File Path (C:/) to log output files

Automating Zerto with PowerShell and REST APIs

7 of 19

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download