Initial auto reconnect
This commit is contained in:
@@ -22,7 +22,11 @@ function Connect-ZertoServer {
|
|||||||
HelpMessage = "Valid credentials to connect to the Zerto Management Server"
|
HelpMessage = "Valid credentials to connect to the Zerto Management Server"
|
||||||
)]
|
)]
|
||||||
[System.Management.Automation.PSCredential]$credential,
|
[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 {
|
begin {
|
||||||
@@ -38,6 +42,10 @@ function Connect-ZertoServer {
|
|||||||
"Accept" = "application/json"
|
"Accept" = "application/json"
|
||||||
"zerto-triggered-by" = "PowershellWes"
|
"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 {
|
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 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"
|
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
|
# Build the URI to be submitted
|
||||||
$submittedURI = "https://{0}:{1}/{2}/{3}" -f $script:zvmServer, $script:zvmPort, $apiVersion, $uri
|
$submittedURI = "https://{0}:{1}/{2}/{3}" -f $script:zvmServer, $script:zvmPort, $apiVersion, $uri
|
||||||
try {
|
try {
|
||||||
@@ -103,6 +105,6 @@ public class TrustAllCertsPolicy : ICertificatePolicy {
|
|||||||
$apiRequestAndHeaderResults | Add-Member -MemberType NoteProperty -Name "apiRequestResults" -Value $apiRequestResults
|
$apiRequestAndHeaderResults | Add-Member -MemberType NoteProperty -Name "apiRequestResults" -Value $apiRequestResults
|
||||||
$apiRequestAndHeaderResults | Add-Member -MemberType NoteProperty -Name "Headers" -Value $responseHeaders
|
$apiRequestAndHeaderResults | Add-Member -MemberType NoteProperty -Name "Headers" -Value $responseHeaders
|
||||||
return $apiRequestAndHeaderResults
|
return $apiRequestAndHeaderResults
|
||||||
}
|
#}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user