Skip to content

Instantly share code, notes, and snippets.

@wcarroll
Created September 21, 2020 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wcarroll/6e7f62f80504ff366bbebc8dd6238d6b to your computer and use it in GitHub Desktop.
Save wcarroll/6e7f62f80504ff366bbebc8dd6238d6b to your computer and use it in GitHub Desktop.
Zerto Virtual Manager Authentication Example for Windows PowerShell
#Requires -PSEdition Desktop
####################################################################################
# This section is required if you are using the default self-signed certificate
# This next block of code will set the PowerShell session to trust all certificates
####################################################################################
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
####################################################################################
# Update the variables below to conform to your environment
####################################################################################
$ZVM = "ZVM FQDN or IP Address"
$Port = 9669 # This is the default, only update if you changed from the default port
$Username = "ENTER A USERNAME"
$Password = "ENTER YOUR PASSWORD"
####################################################################################
# Nothing to Change below this point.
####################################################################################
# Create a PSCredential Object to pass the credential information to the API
$Credential = [pscredential]::new($Username, ($Password | ConvertTo-SecureString -AsPlainText -Force))
# Create a BaseUri string to be used in future calls the the API
$BaseUri = "https://{0}:{1}/v1" -f $ZVM, $Port
# Specify the Authentication endpoint being used
$AuthUri = "{0}/session/add" -f $BaseUri
# Setup our required headers and request parameters
$Headers = @{ Accept = "application/json" }
$RequestParams = @{
Uri = $AuthUri
Headers = $Headers
Method = "POST"
Credential = $Credential
TimeoutSec = 100
ContentType = "application/json"
}
# Send the request and store the response into a variable
$Response = Invoke-WebRequest @RequestParams
# Update our headers variable with the required authentication token
$Headers['x-zerto-session'] = $Response.Headers['x-zerto-session']
# Remove the 'credential' parameter as it is no longer needed
$RequestParams.Remove('Credential')
# Create a URI to test our authentication token setup
$LocalsiteUri = "{0}/localsite" -f $BaseUri
# Update the Request Parameters with the updated URI and New Method
$RequestParams['Uri'] = $LocalsiteUri
$RequestParams['Method'] = "GET"
# Send the updated request and get our localsite information
Invoke-RestMethod @RequestParams
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment