PowerShell Core Example ZVM Authentication Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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