Invoking RESTful APIs from PowerShell Scripts : Authentication Methods
  
Authentication Methods
The following basic authentication methods are available:
Windows-based authentication (default) – The username and password are authenticated by Windows on the host on which the Zerto Virtual Manager or Zerto Cloud Appliance is installed.
Hypervisor manager authentication – The username and password are authenticated by the VMware vCenter Server or Microsoft SCVMM to which the Zerto Virtual Manager is connected.
To authenticate, invoke the following API:
https://zvm-ip:zvm-port/v1/session/add
The response to this request contains a variable called x-zerto-session that contains a unique session ID that is used in subsequent requests to APIs.
Authentication Sample
The following code snippet shows an example of authentication.
 
$baseURL = "https://" + $strZVMIP + ":"+$strZVMPort
5
$xZertoSessionURI = $baseURL +"/v1/session/add"
1
$authInfo = ("{0}:{1}" -f $userName,$password)
2
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
3
$authInfo = [System.Convert]::ToBase64String($authInfo)
4
$headers = @{Authorization=("Basic {0}" -f $authInfo)}
6
$body = '{"AuthenticationMethod": "1"}'
 
$contentType = "application/json"
7
$xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURI -Headers $headers                                            -Method POST -Body $body -ContentType $contentType
8
return $xZertoSessionResponse.headers.get_item("x-zerto-session")
where the code is created using the following steps, with each steps identified by the line number in the above table:
1. Create an authentication object array from the username and password.
$authInfo = ("{0}:{1}" -f $userName,$password)
2. Convert the authentication object to UTF8.
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
3. Convert the information to base64 format.
$authInfo = [System.Convert]::ToBase64String($authInfo)
4. Build the basic authentication format into a header variable.
$headers = @{Authorization=("Basic {0}" -f $authInfo)}
5. Build the session request URL.
$xZertoSessionURI = $baseURL +"/v1/session/add"
6. Authenticate the user's credentials. The fields body and contentType authenticate a user's credentials. By default, Windows authentication is used, and these fields are optional. To authenticate with hypervisor manager credentials, add the following:.
$body = '{"AuthenticationMethod": "1"}'
$contentType = "application/json"
7. Invoke the HTTP request to the specified URL, given the authentication header. The response will contain a header with the x-zerto-session variable.
$xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURI -Headers $headers                                            -Method POST -Body $body -ContentType $contentType
If you are authenticating with the default, Windows credentials, remove “-Body $body -ContentType $contentType”.
8. Extract the x-zerto-session value from the response headers.
$xZertoSessionResponse.headers.get_item("x-zerto-session")