Private/Invoke-ApiRequest.ps1
function Invoke-ApiRequest { <# .SYNOPSIS Send an API request to the Zerto Virtual Manager server. .DESCRIPTION This is a helper function which uses the "$Global:ZvmSession.BaseURL" and "$Global:ZvmSession.SessionToken" global variables to build the HTTP request path and set the x-zerto-session header. .PARAMETER Method Specify the HTTP method to use. .PARAMETER Path Specify the request path. .PARAMETER Body Specify the HTTP request body. .EXAMPLE Invoke-ApiRequest -Path "v1/vpgs" -Method "GET" .EXAMPLE $Body = @{ "checkpointName" = "Some checkpoint name string" } | ConvertTo-Json Invoke-ApiRequest -Path "v1/vpgs/<vpg_id>/checkpoints" -Method "POST" -Body $Body #> param ( [String] $Method = "GET", [Uri] $Path, [String] $Body ) try { if (!($Global:ZvmSession.SessionToken)) { throw "You are not connected to a ZVM server." } $Headers = @{ "x-zerto-session" = $Global:ZvmSession.SessionToken } $ApiUri = "$($Global:ZvmSession.BaseURL)/$Path" $Params = @{ Uri = $ApiUri Headers = $Headers Method = $Method ContentType = "application/json" } if (!($Method -eq "GET")) { $Params.Body = $Body } $Response = Invoke-RestMethod @Params $Response } catch { $Err = $_ if ($Err.ErrorDetails.Message -like "*Invalid session*") { throw "Invalid or expired session token!" } else { throw $Err } } } |