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

Welcome Forums General PowerShell Q&A Invoke-RestMethod works fine on PowershellCore 7, but got error on Powershell 5

Viewing 2 reply threads
  • Author
    Posts
    • #239870
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Hi community,

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

      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:

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

      Thanks in advance.

      • This topic was modified 2 months, 2 weeks ago by Olair Soares.
    • #239873
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      Olair, welcome to Powershell.org. 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:

      Force the Invoke-RestMethod PowerShell cmdlet to use TLS 1.2

    • #239882
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Hi Rob,

      Thanks for your reply.

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

      .

      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.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.