Update connection to work with expired token
This commit is contained in:
@@ -15,19 +15,14 @@ function Invoke-ZertoRestRequest {
|
||||
Write-Error -Message "Zerto Connection does not Exist. Please run Connect-ZertoServer first to establish a connection"
|
||||
break
|
||||
}
|
||||
if ( Test-Path variable:script:zvmHeaders ) {
|
||||
$headers = $script:zvmHeaders
|
||||
} else {
|
||||
$headers = $null
|
||||
}
|
||||
if ( (Test-Path variable:script:zvmHeaders) -and $([datetime]$script:zvmLastAction).addMinutes(30) -lt $(get-date) ) {
|
||||
Write-Error -Message "Authorization Token has Expired or Does not exist in Env variables. Please re-authorize to the Zerto Virtual Manager"
|
||||
Write-Error -Message "Authorization Token has Expired. Please re-authorize to the Zerto Virtual Manager"
|
||||
break
|
||||
} else {
|
||||
$submittedURI = "https://{0}:{1}/{2}/{3}" -f $script:zvmServer, $script:zvmPort, $apiVersion, $uri
|
||||
try {
|
||||
$script:zvmLastAction = (get-date).Ticks
|
||||
$apiRequestResults = Invoke-RestMethod -Uri $submittedURI -Headers $headers -Method $method -Body $body -ContentType $contentType -Credential $credential -SkipCertificateCheck -ResponseHeadersVariable responseHeaders -TimeoutSec 100
|
||||
$apiRequestResults = Invoke-RestMethod -Uri $submittedURI -Headers $script:zvmHeaders -Method $method -Body $body -ContentType $contentType -Credential $credential -SkipCertificateCheck -ResponseHeadersVariable responseHeaders -TimeoutSec 100
|
||||
} catch {
|
||||
Write-Error -ErrorRecord $_ -ErrorAction $callerErrorActionPreference
|
||||
}
|
||||
|
||||
@@ -16,20 +16,20 @@ function Connect-ZertoServer {
|
||||
HelpMessage = "Valid credentials to connect to the Zerto Management Server"
|
||||
)]
|
||||
[System.Management.Automation.PSCredential]
|
||||
$credential
|
||||
$credential,
|
||||
[switch]$returnHeaders
|
||||
)
|
||||
Set-Variable -Name zvmServer -Scope Script -Value $zertoServer
|
||||
Set-Variable -Name zvmPort -Scope Script -Value $zertoPort
|
||||
Set-Variable -Name zvmLastAction -Scope Script -Value $(get-date).Ticks
|
||||
# $zertoConnectionInformation = @{"zertoServer" = $zertoServer; "zertoPort" = $zertoPort; "LastAction" = $(get-date).Ticks}
|
||||
# Set-Item Env:zertoConnectionInformation -Value ($zertoConnectionInformation | ConvertTo-Json -Compress)
|
||||
Set-Variable -Name zvmHeaders -Scope Script -Value $null
|
||||
$body = '{"AuthenticationMethod": "1"}'
|
||||
$uri = "session/add"
|
||||
$results = Invoke-ZertoRestRequest -uri $uri -credential $credential -returnHeaders -body $body -method POST
|
||||
$zertoAuthorizationHeaders = @{"x-zerto-session" = $results.Headers['x-zerto-session'][0].ToString(); "Accept" = "application/json"}
|
||||
Set-Variable -Name zvmHeaders -Scope Script -Value $zertoAuthorizationHeaders
|
||||
# Set-Item Env:zertoAuthorizationHeaders -Value ($zertoAuthorizationHeaders | ConvertTo-Json -Compress)
|
||||
Set-Variable -Name zvmLocalInfo -Scope Script -Value (Get-ZertoLocalSite)
|
||||
# Set-Item Env:zertoLocalSiteInfo -Value ($zertoLocalSiteInfo | ConvertTo-Json -Compress)
|
||||
return $zertoAuthorizationHeaders
|
||||
if ($returnHeaders) {
|
||||
return $zertoAuthorizationHeaders
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user