diff --git a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 index 599bad8..70e8951 100644 --- a/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 +++ b/ZertoApiWrapper/Private/Invoke-ZARestRequest.ps1 @@ -7,14 +7,26 @@ function Invoke-ZARestRequest { [string]$contentType = "application/json" ) - $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 + # Check to see if the required variables are present and currently valid + if ( -not ((Test-Path variable:script:zaLastActionTime) -and (Test-Path variable:script:zaHeaders)) ) { + 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 { - if ([String]::IsNullOrEmpty($body)) { - Invoke-RestMethod -Uri $submittedUri -Method $method -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 + # Update the last action time and submit the request based on PS Version. + 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 { - 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 + } } } -} \ No newline at end of file +}