From dc08618ae90ba5fe5561c18edea7df84d6b6b474 Mon Sep 17 00:00:00 2001 From: Justin Paul Date: Sun, 9 Feb 2025 20:47:47 -0500 Subject: [PATCH] Update Invoke-ZertoRestRequest.ps1 --- .../Public/Invoke-ZertoRestRequest.ps1 | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ZertoApiWrapper/Public/Invoke-ZertoRestRequest.ps1 b/ZertoApiWrapper/Public/Invoke-ZertoRestRequest.ps1 index f44e15d..a899434 100644 --- a/ZertoApiWrapper/Public/Invoke-ZertoRestRequest.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZertoRestRequest.ps1 @@ -80,12 +80,28 @@ function Invoke-ZertoRestRequest { $params.ContentType = "application/x-www-form-urlencoded" } - # Handle certificate validation for PowerShell 5.1 - if ($PSVersionTable.PSVersion.Major -ge 6) { + # Handle SSL/TLS trust issue for PowerShell 5.1 + if ($PSVersionTable.PSVersion.Major -lt 6) { + Add-Type -TypeDefinition @" + using System; + using System.Net; + using System.Security.Cryptography.X509Certificates; + public class TrustAllCertsPolicy : ICertificatePolicy { + public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { + return true; + } + } +"@ -PassThru + [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy + } else { $params["SkipCertificateCheck"] = $true + } + + # Execute API request + if ($PSVersionTable.PSVersion.Major -ge 6) { $apiRequestResults = Invoke-RestMethod @params -ResponseHeadersVariable responseHeaders } else { - # PowerShell 5.1 workaround since `-ResponseHeadersVariable` is not available + # PowerShell 5.1 workaround: use Invoke-WebRequest instead $webResponse = Invoke-WebRequest @params $apiRequestResults = $webResponse.Content | ConvertFrom-Json -ErrorAction SilentlyContinue $responseHeaders = $webResponse.Headers