Skip to content

Instantly share code, notes, and snippets.

@wcarroll
Last active March 10, 2021 00:07
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/c6f9bf7a152499b29ef9003dfd5d0016 to your computer and use it in GitHub Desktop.
Save wcarroll/c6f9bf7a152499b29ef9003dfd5d0016 to your computer and use it in GitHub Desktop.
PowerShell Core Example ZVM Authentication Script
#Requires -PSEdition Core
####################################################################################
# Update the variables below to conform to your environment
####################################################################################
$ZVM = "nczvm.nc.lab"
$Port = 9669 # This is the default, only update if you changed from the default port
$Username = "wcarroll@nc.lab"
$Password = "MySuperSecretPassword"
####################################################################################
# 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))
# 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"
ResponseHeadersVariable = "responseHeaders"
SkipCertificateCheck = $True
}
# Send the request
Invoke-RestMethod @RequestParams
# Update our headers variable with the required authentication token
$Headers['x-zerto-session'] = $responseHeaders['x-zerto-session'][0]
# 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