Update Invoke-ZertoRestRequest.ps1

This commit is contained in:
Wes Carroll
2019-07-18 21:25:18 -04:00
parent 6b05af2db4
commit 1c17ca3e96
@@ -1,26 +1,29 @@
function Invoke-ZertoRestRequest { function Invoke-ZertoRestRequest {
[cmdletbinding()] [cmdletbinding()]
param( param(
[ValidateSet("GET", "PUT", "POST", "DELETE")]
[string]$method = "GET", [string]$method = "GET",
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$uri, [string]$uri,
[ValidateNotNullOrEmpty()]
[string]$apiVersion = "v1", [string]$apiVersion = "v1",
[ValidateNotNullOrEmpty()]
[string]$body, [string]$body,
[ValidateNotNullOrEmpty()]
[string]$contentType = "application/json", [string]$contentType = "application/json",
[System.Management.Automation.PSCredential] [PSCredential]$credential,
$credential,
[switch]$returnHeaders [switch]$returnHeaders
) )
$callerErrorActionPreference = $ErrorActionPreference $callerErrorActionPreference = $ErrorActionPreference
# If the ZVM server and Port not defined, Stop Call # If the ZVM server and Port not defined, Stop Call
if ( -not ((Test-Path variable:script:zvmServer) -and (Test-Path variable:script:zvmPort)) ) { if ( -not ((Test-Path variable:script:zvmServer) -and (Test-Path variable:script:zvmPort)) ) {
Write-Error -Message "Zerto Connection does not Exist. Please run Connect-ZertoServer first to establish a connection" ThrowError -ExceptionName "NoConnection" -ExceptionMessage "Zerto Connection does not Exist. Please run Connect-ZertoServer first to establish a connection" -errorId "Connection01" -errorCategory "Connection" -ErrorAction Stop
break
} }
# If the Headers exist and the Last action was more than 30 minutes ago, Session is Expired # If the Headers exist and the Last action was more than 30 minutes ago, Session is Expired
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. Please re-authorize to the Zerto Virtual Manager" ThrowError -ExceptionName "ExpiredToken" -ExceptionMessage "Authorization Token has Expired. Please re-authorize to the Zerto Virtual Manager" -errorId "ZVMAuth01" -errorCategory "Auth" -ErrorAction Stop
break
} else { } else {
# Build the URI to be submitted # Build the URI to be submitted
@@ -57,7 +60,7 @@ public class TrustAllCertsPolicy : ICertificatePolicy {
# If we are authenticating to the ZVM, Use this block to use Invoke-WebRequest and format the Headers as expected. # If we are authenticating to the ZVM, Use this block to use Invoke-WebRequest and format the Headers as expected.
if ($uri -eq "session/add" -and $method -eq "POST") { if ($uri -eq "session/add" -and $method -eq "POST") {
$apiRequestResults = Invoke-WebRequest -Uri $submittedURI -Headers $script:zvmHeaders -Method $method -Body $body -ContentType $contentType -Credential $credential -TimeoutSec 100 $apiRequestResults = Invoke-WebRequest -Uri $submittedURI -Headers $script:zvmHeaders -Method $method -Body $body -ContentType $contentType -Credential $credential -TimeoutSec 100
$responseHeaders = @{} $responseHeaders = @{ }
$responseHeaders['x-zerto-session'] = @($apiRequestResults.Headers['x-zerto-session']) $responseHeaders['x-zerto-session'] = @($apiRequestResults.Headers['x-zerto-session'])
} elseif ($method -ne "GET") { } elseif ($method -ne "GET") {
# If the Method is something other than 'GET' use this call with a body parameter # If the Method is something other than 'GET' use this call with a body parameter