Update to ThrowErrors when required vars missing

This commit is contained in:
Wes Carroll
2019-07-16 14:16:06 -04:00
parent db91acb770
commit 40326da05f
@@ -1,19 +1,23 @@
function Invoke-ZARestRequest { function Invoke-ZARestRequest {
[cmdletbinding()] [cmdletbinding()]
param( param(
# Parameter help description
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$uri, [string]$uri,
[ValidateSet("GET", "PUT", "POST", "DELETE")]
[string]$method = "GET", [string]$method = "GET",
[ValidateNotNullOrEmpty()]
[string]$body, [string]$body,
[ValidateNotNullOrEmpty()]
[string]$contentType = "application/json" [string]$contentType = "application/json"
) )
# Check to see if the required variables are present and currently valid # 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)) ) { 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" ThrowError -ExceptionName "NoAuth" -ExceptionMessage "Zerto Analytics Connection does not Exist. Please run Connect-ZertoAnalytics first to establish a connection" -errorId "Auth02" -errorCategory "ZertoAuth" -ErrorAction Stop
break
} elseif ( (Test-Path variable:script:zaHeaders) -and $([datetime]$script:zaLastActionTime).addMinutes(60) -lt $(get-date) ) { } elseif ( (Test-Path variable:script:zaHeaders) -and $([datetime]$script:zaLastActionTime).addMinutes(60) -lt $(get-date) ) {
Write-Error -Message "Authorization Token has Expired. Please re-authorize to the Zerto Analytics Portal" ThrowError -ExceptionName "ExpiredToken" -ExceptionMessage "Authorization Token has Expired. Please re-authorize to the Zerto Analytics Portal" -errorId "Auth01" -errorCategory "ZertoAuth" -ErrorAction Stop
break
} else { } else {
# Update the last action time and submit the request based on PS Version. # Update the last action time and submit the request based on PS Version.
Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-date).Ticks Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-date).Ticks