Invoking RESTful APIs from PowerShell Scripts : Example: Perform an Action
  
Example: Perform an Action
The following code sample creates a VPG with a list of VMs specified in a file.
#Parameters Section
$strZVMIP = "{Zerto Virtual Manager IP}"
$strZVMPort = "{Zerto Virtual Manager HTTPS port}"
$strZVMUser = "{Zerto Virtual Manager user}"
$strZVMPw = "{Zerto Virtual Manager user password}"
$sourceSiteName = "{protected site name}"
$targetSiteName = "{recovery site name}"
$targetDataStoreName = "{recovery storage name in the recovery site for the VPG}"
$vpgName = "{name of the VPG you want to create}"
$unProtectedVMsCSVFile = "name of the file that has the names of the VMs to add to the VPG. The file must not have headers, and the VM names must be separated with commas, without spaces between the names. For example, the first row in the file would look like this: vm1,vm2,vm3}"
$BASEURL = "https://" + $strZVMIP + ":"+$strZVMPort+"/v1/" #base URL for all APIs
##Function Definitions
#Get a site identifier by invoking Zerto APIs, given a Zerto API session and a site name:
function getSiteIdentifierByName ($sessionHeader, $siteName){
  $url = $BASEURL + "virtualizationsites"
  $response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/xml"
  ForEach ($site in $response.ArrayOfVirtualizationSiteApi.VirtualizationSiteApi) {
    if ($site.VirtualizationSiteName -eq $siteName){
      return $site.SiteIdentifier
    }
  }
}
#Get a storage identifier by invoking Zerto APIs, given a Zerto Virtual Replication API session and a storage name:
function getDatastoreIdentifierByName ($sessionHeader, $siteIdentfier, $datastoreName){
  $url = $BASEURL + "virtualizationsites/"+$siteIdentfier + "/datastores"
  $response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/xml"
  ForEach ($datastore in $response.ArrayOfDatastoreNativeApi.DatastoreNativeApi) {
    if ($datastore.DatastoreName -eq $datastoreName){
      return $datastore.DatastoreIdentifier
    }
  }
}
#Get unprotected VM identifiers by invoking Zerto APIs, given a Zerto API session, a site identifier, and a list of VMs to add to the VPG:
function getUnprotectedVMsIdentifiers($sessionHeader, $siteIdentfier, $VMNames){
  $url = $BASEURL + "virtualizationsites/"+$siteIdentfier + "/vms"
  $unprotectedVMsIdentifiers = @()
  $response = Invoke-RestMethod -Uri $url -Headers $zertoSessionHeader -ContentType "application/xml"
  ForEach ($vm in $response.ArrayOfVmNativeApi.VmNativeApi) {
    if ($VMNames.IndexOf($vm.VmName) -gt -1){
      $unprotectedVMsIdentifiers+=($vm.VmIdentifier)
    }
  }
  return $unprotectedVMsIdentifiers
}
#Authenticate with Zerto APIs: create a Zerto API session and return it, to be used in other APIs
function getZertoXSession (){
  #Authenticate with Zerto APIs:
  $xZertoSessionURI = "https://" + $strZVMIP + ":"+$strZVMPort+"/v1/session/add"
  $authInfo = ("{0}:{1}" -f $strZVMUser,$strZVMPw)
  $authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
  $authInfo = [System.Convert]::ToBase64String($authInfo)
  $headers = @{Authorization=("Basic {0}" -f $authInfo)}
  $xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURI -Headers $headers -Method POST
  #Extract x-zerto-session from the response and add it to the actual API:
  $xZertoSession = $xZertoSessionResponse.headers.get_item("x-zerto-session")
  return $xZertoSession
}
#Build VM elements to be added to the VPGs API, based on a list of VM identifiers
function buildVMsElement ($VMs) {
$response = "<VmsIdentifiers>"
 
  ForEach ($vm in $VMs) {
    $response+="<string xmlns="+'"http://schemas.microsoft.com/2003/10/Serialization/Arrays"'+">"+$vm+"</string>"
  }
  $response += "</VmsIdentifiers>"
  return $response
}
#Script starts here:
$xZertoSession = getZertoXSession
 
$zertoSessionHeader = @{"x-zerto-session"=$xZertoSession}
 
$sourceSiteIdentifier = getSiteIdentifierByName $zertoSessionHeader $sourceSiteName
 
$targetSiteIdentifier = getSiteIdentifierByName $zertoSessionHeader $targetSiteName
 
$dataStoreIdentifier = getDatastoreIdentifierByName $zertoSessionHeader $targetSiteIdentifier $targetDataStoreName
 
$unprotectedVMNames = Get-Content $unProtectedVMsCSVFile | %{$_.Split(",")}
 
$vmsIdentifiers = getUnprotectedVMsIdentifiers $zertoSessionHeader $sourceSiteIdentifier $unprotectedVMNames
 
$vmsIdentifiersElement = buildVMsElement $vmsIdentifiers
#Create the URL and body of the VPGs request:
$createVPGUrl = $BASEURL+"vpgs"
$vpgsRequestBody = "<VpgCreateDataApi xmlns="+'"http://schemas.zerto.com/zvm/api"'+">"
            +"<DatastoreIdentifier>"+$dataStoreIdentifier +"</DatastoreIdentifier>
            <SourceSiteIdentifier>"+$sourceSiteIdentifier+"</SourceSiteIdentifier>
            <TargetSiteIdentifier>"+$targetSiteIdentifier+"</TargetSiteIdentifier>"
            +$vmsIdentifiersElement+"<VpgName>"+$vpgName+"</VpgName> </VpgCreateDataApi>"
#Invoke the Zerto API:
Invoke-RestMethod -Uri $createVPGUrl -Headers $zertoSessionHeader
                    -Body $vpgsRequestBody -ContentType "application/xml" -method POST
##End of script