Update connection to work with expired token

This commit is contained in:
Wes Carroll
2019-02-22 13:29:38 -05:00
parent 94a54f013a
commit 9fd116b36f
2 changed files with 8 additions and 13 deletions
@@ -15,19 +15,14 @@ function Invoke-ZertoRestRequest {
Write-Error -Message "Zerto Connection does not Exist. Please run Connect-ZertoServer first to establish a connection" Write-Error -Message "Zerto Connection does not Exist. Please run Connect-ZertoServer first to establish a connection"
break 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) ) { 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 break
} else { } else {
$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 {
$script:zvmLastAction = (get-date).Ticks $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 { } catch {
Write-Error -ErrorRecord $_ -ErrorAction $callerErrorActionPreference Write-Error -ErrorRecord $_ -ErrorAction $callerErrorActionPreference
} }
@@ -16,20 +16,20 @@ 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] [System.Management.Automation.PSCredential]
$credential $credential,
[switch]$returnHeaders
) )
Set-Variable -Name zvmServer -Scope Script -Value $zertoServer Set-Variable -Name zvmServer -Scope Script -Value $zertoServer
Set-Variable -Name zvmPort -Scope Script -Value $zertoPort Set-Variable -Name zvmPort -Scope Script -Value $zertoPort
Set-Variable -Name zvmLastAction -Scope Script -Value $(get-date).Ticks Set-Variable -Name zvmLastAction -Scope Script -Value $(get-date).Ticks
# $zertoConnectionInformation = @{"zertoServer" = $zertoServer; "zertoPort" = $zertoPort; "LastAction" = $(get-date).Ticks} Set-Variable -Name zvmHeaders -Scope Script -Value $null
# Set-Item Env:zertoConnectionInformation -Value ($zertoConnectionInformation | ConvertTo-Json -Compress)
$body = '{"AuthenticationMethod": "1"}' $body = '{"AuthenticationMethod": "1"}'
$uri = "session/add" $uri = "session/add"
$results = Invoke-ZertoRestRequest -uri $uri -credential $credential -returnHeaders -body $body -method POST $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"} $zertoAuthorizationHeaders = @{"x-zerto-session" = $results.Headers['x-zerto-session'][0].ToString(); "Accept" = "application/json"}
Set-Variable -Name zvmHeaders -Scope Script -Value $zertoAuthorizationHeaders 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-Variable -Name zvmLocalInfo -Scope Script -Value (Get-ZertoLocalSite)
# Set-Item Env:zertoLocalSiteInfo -Value ($zertoLocalSiteInfo | ConvertTo-Json -Compress) if ($returnHeaders) {
return $zertoAuthorizationHeaders return $zertoAuthorizationHeaders
}
} }