Initial auto reconnect

This commit is contained in:
Wes Carroll
2020-06-08 12:50:29 -04:00
parent 52609bf64a
commit 3000a2b013
2 changed files with 60 additions and 50 deletions
@@ -22,7 +22,11 @@ function Connect-ZertoServer {
HelpMessage = "Valid credentials to connect to the Zerto Management Server"
)]
[System.Management.Automation.PSCredential]$credential,
[switch]$returnHeaders
[switch]$returnHeaders,
[Parameter(
HelpMessage = "Use this switch to indicate that you would like the module to take care of auto re-authorization and reconnection to the ZVM should the token expire. This option will cache your PSCredential object to be reused"
)]
[switch]$AutoReconnect
)
begin {
@@ -38,6 +42,10 @@ function Connect-ZertoServer {
"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
}
}
process {
@@ -41,10 +41,12 @@ 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) ) {
if ( (Test-Path variable:script:zvmHeaders) -and $([datetime]$script:zvmLastAction).addMinutes(30) -lt $(Get-Date) -and $Script:Reconnect -eq $False ) {
Throw "Authorization Token has Expired. Please re-authorize to the Zerto Virtual Manager"
} else {
} elseif (( (Test-Path variable:script:zvmHeaders) -and $([datetime]$script:zvmLastAction).addMinutes(30) -lt $(Get-Date) -and $Script:Reconnect -eq $True )) {
Write-Verbose "Authorization had expired. Attempting Reauthorization."
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 {
@@ -103,6 +105,6 @@ public class TrustAllCertsPolicy : ICertificatePolicy {
$apiRequestAndHeaderResults | Add-Member -MemberType NoteProperty -Name "apiRequestResults" -Value $apiRequestResults
$apiRequestAndHeaderResults | Add-Member -MemberType NoteProperty -Name "Headers" -Value $responseHeaders
return $apiRequestAndHeaderResults
}
#}
}
}