Add Logic for token tests
This commit is contained in:
@@ -7,14 +7,26 @@ function Invoke-ZARestRequest {
|
|||||||
[string]$contentType = "application/json"
|
[string]$contentType = "application/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
$submittedUri = "https://analytics.api.zerto.com/v2/{0}" -f $uri
|
# Check to see if the required variables are present and currently valid
|
||||||
if ($PSVersionTable.PSVersion.Major -ge 6) {
|
if ( -not ((Test-Path variable:script:zaLastActionTime) -and (Test-Path variable:script:zaHeaders)) ) {
|
||||||
Invoke-RestMethod -Uri $submittedUri -Method $method -Body $body -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100
|
Write-Error -Message "Zerto Analytics Connection does not Exist. Please run Connect-ZertoAnalytics first to establish a connection"
|
||||||
|
break
|
||||||
|
} elseif ( (Test-Path variable:script:zaHeaders) -and $([datetime]$script:zaLastAction).addMinutes(60) -lt $(get-date) ) {
|
||||||
|
Write-Error -Message "Authorization Token has Expired. Please re-authorize to the Zerto Virtual Manager"
|
||||||
|
break
|
||||||
} else {
|
} else {
|
||||||
if ([String]::IsNullOrEmpty($body)) {
|
# Update the last action time and submit the request based on PS Version.
|
||||||
Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100
|
Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-date).Ticks
|
||||||
|
$submittedUri = "https://analytics.api.zerto.com/v2/{0}" -f $uri
|
||||||
|
if ($PSVersionTable.PSVersion.Major -ge 6) {
|
||||||
|
Invoke-RestMethod -Uri $submittedUri -Method $method -Body $body -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100
|
||||||
} else {
|
} else {
|
||||||
Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 -Body $body
|
# With PS 5, you cannot ship a $null body, check for $body variable and select correct Invoke request.
|
||||||
|
if ([String]::IsNullOrEmpty($body)) {
|
||||||
|
Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100
|
||||||
|
} else {
|
||||||
|
Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 -Body $body
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user