Invoke-RestMethod works fine on PowershellCore 7, but got error on Powershell 5

Hi community,

I got an issue with Invoke-RestMethod. Basically I have the script below:

$thresholdUrl = ""

$Customerheaders = @{
'Accept' = "application/json; version=2.1"
'Authorization' = "Zoho-oauthtoken 1000.ab5dba56df9188cec0f57d1ff8ad509a.200613a3f700394bc169a0d8aa345XXX"
'Cookie' = "zaaid=20071266249"

#get a list of Threshold Profiles to that specific customer

$thresholds = Invoke-RestMethod -Method 'get' -Uri $thresholdUrl -Headers $Customerheaders

When I run from Powershell Core7 on my windows 10, it works fine and I receive a answer from the API. But when I try to run the same script from Powershell 5.1, I’ve got the below error:

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
At C:\Users\olair.soares\Create
ProfilesV4.ps1:12 char:15
+ ... hresholds = Invoke-RestMethod -Method 'get' -Uri $thresholdUrl -Heade ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Does anyone knows what is the issue? I need to be able to run it from Powershell 5.1 due compatibility.

Thanks in advance.

Olair, welcome to Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
Thanks in advance.

The error is unauthorized, so that would indicate something is amiss with your Authorization. There isn’t anything in the code that is a compatibility issue between 5 and 7. You can try to set the TLS version, but it appears to be something with authorization:

Hi Rob,

Thanks for your reply.

I already tested including the below line on the top of script:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

The behavior is the same, I tested from my workstation and other test VM. I’ve the Powershell 5.1 and Powershell Core 7 running on both Machine. I get the error when running from Powershell 5.1, but it run fine from Powershell Core 7 on both machines. I’m not a experient powershell guy, but I guess that there is any change on the way that cmdlet call the url.