beta for zvma10

This update has all the code needed to connect to a zvma 10.0 (and probabaly 9.7) and allow the end user to connect to zvma as well as run all of the other functions.
This commit is contained in:
2024-01-23 13:24:17 -05:00
parent 61b09638ab
commit 181761310c
10 changed files with 71 additions and 53 deletions
@@ -1,4 +1,5 @@
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #>
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml
#>
function Connect-ZertoAnalytics {
[cmdletbinding()]
param(
+7 -10
View File
@@ -1,3 +1,5 @@
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml
#>
function Connect-ZertoServer {
[cmdletbinding()]
[OutputType([hashtable])]
@@ -20,7 +22,7 @@ function Connect-ZertoServer {
HelpMessage = "Zerto Virtual Manager management port. Default value is 443."
)]
[ValidateNotNullOrEmpty()]
[ValidateRange(1024, 65535)]
[ValidateRange(443, 65535)]
[Alias("port")]
[string]$zertoPort = "443",
[Parameter(
@@ -45,30 +47,25 @@ function Connect-ZertoServer {
# Set Script Scope Variables for Use in all functions in the module; Server and Port Information
Set-Variable -Name zvmServer -Scope Script -Value $zertoServer
Set-Variable -Name zvmPort -Scope Script -Value $zertoPort
Set-Variable -Name zvmClientId -Scope Script -Value $zertoClientId
# Set zvmLastAction Variable to keep track when the API token expires
Set-Variable -Name zvmLastAction -Scope Script -Value $(Get-Date).Ticks
# Set / Clear the zvmHeaders to clear any existing token
Set-Variable -Name zvmHeaders -Scope Script -Value @{
#"Accept" = "application/json"
"Accept" = "application/json"
"zerto-triggered-by" = "PowershellWes"
}
Set-Variable -Name Reconnect -Scope Script -Value $AutoReconnect.IsPresent
if ($Script:Reconnect) {
Set-Variable -Name CachedCredential -Scope Script -Value $credential
}
# need to check to see if we need this or if the zvmclientid above is enough
Set-Variable -Name zertoClientId -Scope Script -Value $zertoClientId
$body = @{
'client_id' = $script:zertoClientId
'username' = $credential.GetNetworkCredential().Username
'password' = $credential.GetNetworkCredential().Password
'grant_type' = 'password'
}
}
process {
# Send authorization request to the function and send back the results including headers -returnHeaders
$results = Invoke-ZertoRestRequest -uri $uri -credential $credential -body $body -method POST -ErrorAction Stop
$results = Invoke-ZertoRestRequest -uri $uri -credential $credential -method POST -ErrorAction Stop
}
end {
@@ -1,11 +1,12 @@
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #>
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml
#>
function Disconnect-ZertoServer {
[cmdletbinding()]
param()
$uri = "session"
$uri = "auth/realms/zerto/protocol/openid-connect/logout"
# Delete API Authorization
$null = Invoke-ZertoRestRequest -uri $uri -method DELETE
$null = Invoke-ZertoRestRequest -uri $uri -method POST
# Remove all variables used
Remove-Variable -Name zvmServer -Scope Script
@@ -40,12 +40,15 @@ function Invoke-ZertoRestRequest {
}
# If the Headers exist and the Last action was more than 30 minutes ago, Session is Expired
if ( (Test-Path variable:script:zvmHeaders) -and $([datetime]$script:zvmLastAction).addMinutes(30) -lt $(Get-Date) -and $Script:Reconnect -eq $False ) {
if ( (Test-Path variable:script:zvmHeaders) -and (Test-Path variable:script:AuthExpiresAt) -and $([datetime]$script:AuthExpiresAt) -lt $(Get-Date) -and $Script:Reconnect -eq $False ) {
Remove-Variable -Name AuthExpiresAt -Scope Script
Throw "Authorization Token has Expired. Please re-authorize to the Zerto Virtual Manager"
} elseif (( (Test-Path variable:script:zvmHeaders) -and $([datetime]$script:zvmLastAction).addMinutes(30) -lt $(Get-Date) -and $Script:Reconnect -eq $True )) {
} elseif (( (Test-Path variable:script:zvmHeaders) -and (Test-Path variable:script:AuthExpiresAt) -and $([datetime]$script:AuthExpiresAt) -lt $(Get-Date) -and $Script:Reconnect -eq $True )) {
Write-Verbose "Authorization had expired. Attempting Reauthorization."
Remove-Variable -Name AuthExpiresAt -Scope Script
Connect-ZertoServer -zertoServer $Script:zvmServer -zertoPort $script:zvmPort -credential $Script:CachedCredential
}# else {
# Build the URI to be submitted
$submittedURI = "https://{0}:{1}/{2}/{3}" -f $script:zvmServer, $script:zvmPort, $apiVersion, $uri
try {
@@ -53,16 +56,38 @@ function Invoke-ZertoRestRequest {
$script:zvmLastAction = (Get-Date).Ticks
# If running PwSh - Use this Invoke-RestMethod with passed Variables
if ($PSVersionTable.PSVersion.Major -ge 6) {
# If we are authenticating to the ZVM, Use this block to use Invoke-WebRequest and format the Headers as expected.
# If we are authenticating to the ZVM, Use this block to use Invoke-WebRequest and format the Headers and Body as expected.
if ($uri -eq "auth/realms/zerto/protocol/openid-connect/token" -and $method -eq "POST") {
$data = @{
'client_id' = 'zerto-client'
'username' = 'admin'
'password' = 'Zertodata987!'
'client_id' = $script:zertoClientId
'username' = $credential.GetNetworkCredential().UserName
'password' = $credential.GetNetworkCredential().Password
'grant_type' = 'password'
}
$params = @{
'Uri' = 'https://192.168.50.60/auth/realms/zerto/protocol/openid-connect/token'
'Uri' = 'https://' + $script:zvmServer + ':' + $script:zvmPort + '/auth/realms/zerto/protocol/openid-connect/token'
'Method' = 'Post'
'Body' = $data
'ContentType' = 'application/x-www-form-urlencoded'
}
$apiRequestResults = Invoke-RestMethod @params -SkipCertificateCheck
$ExpiresIn = $apiRequestResults.expires_in
$script:AuthExpiresAt = (Get-Date).AddSeconds($ExpiresIn)
$script:refreshToken = $apiRequestResults.refresh_token
$responseHeaders = @{ }
$responseHeaders['Authorization'] = "Bearer " + @($apiRequestResults.access_token)
# If we are logging out from the ZVM, use this block to use Invoke-WebRequest and format the Headers and Body as expected.
} elseif ($uri -eq "auth/realms/zerto/protocol/openid-connect/logout" -and $method -eq "POST") {
$data = @{
'client_id' = $script:zertoClientId
'logout' = 'true'
}
$params = @{
'Uri' = 'https://' + $script:zvmServer + ':' + $script:zvmPort + '/auth/realms/zerto/protocol/openid-connect/logout'
'Method' = 'Post'
'Body' = $data
'ContentType' = 'application/x-www-form-urlencoded'
@@ -70,12 +95,6 @@ function Invoke-ZertoRestRequest {
$apiRequestResults = Invoke-RestMethod @params -SkipCertificateCheck
$ExpiresIn = $apiRequestResults.expires_in
$script:AuthExpiresAt = (Get-Date).AddSeconds($ExpiresIn)
$script:refreshToken = $apiRequestResults.refresh_token
$responseHeaders = @{ }
$responseHeaders['Authorization'] = "Bearer " + @($apiRequestResults.access_token)
} else {
$apiRequestResults = Invoke-RestMethod -Uri $submittedURI -Headers $script:zvmHeaders -Method $method -Body $body -ContentType $contentType -Credential $credential -SkipCertificateCheck -ResponseHeadersVariable responseHeaders -TimeoutSec 100
}
+1 -1
View File
@@ -12,7 +12,7 @@
RootModule = '.\ZertoApiWrapper.psm1'
# Version number of this module.
ModuleVersion = '0.0.1'
ModuleVersion = '2.0.0'
# Supported PSEditions
# CompatiblePSEditions = @()