Invoke-RestMethod (GET) : The remote server returned an error: (403) Forbidden PowerShell but some work with CMD Curl

177 views Asked by At

The same Error happened with the 'get' method in the PowerShell command Invoke-RestMethod. I'm wondering if someone can help me with the following error (hide some information):


PS C:\Windows\system32> Invoke-RestMethod -Uri 'https://XXXXXXXXXXXXXXX.XXXX.XXX/YYYYY/ZZZZ/AAAA/BBBBB' -Method Get -Headers @{ >> 'Cookie' = 'AuthToken=XXXXXXXXXXXXXXXXXXXXXXXXX;' >> 'Referer' = 'https://XXXXXXXXX.XXXXX.XXXX/YYYYY/ZZZZZ/CCCCCC'} 

Invoke-RestMethod : The remote server returned an error: (403) Forbidden. At line:1 char:1 + Invoke-RestMethod -Uri 'https://XXXXXXXXX.XXXXXX.XXXX/YYYYY/ZZZZZ/AAA ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc eption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Following works at cmd CURL:

curl -i -k -X GET https://XXXXXXXXXXXXXXX.XXXX.XXX/YYYYY/ZZZZ/AAAA/BBBBB -H "Cookie: AuthToken=XXXXXXXXXXXXXXXXXXXXXXXXX;" -H "Referer: https://XXXXXXXXX.XXXXX.XXXX/YYYYY/ZZZZZ/CCCCCC"

Powershell version


PS C:\Windows\system32> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      22621  2506

I try to add -SkipCertificateCheck at the end but get some different error...

PS C:\Windows\system32> Invoke-RestMethod -Uri 'https://XXXXXXXXXXXXXXX.XXXX.XXX/YYYYY/ZZZZ/AAAA/BBBBB' -Method Get -Headers @{ >> 'Cookie' = 'AuthToken=XXXXXXXXXXXXXXXXXXXXXXXXX;' >> 'Referer' = 'https://XXXXXXXXX.XXXXX.XXXX/YYYYY/ZZZZZ/CCCCCC'}  -SkipCertificateCheck

Invoke-RestMethod : A parameter cannot be found that matches parameter name 'SkipCertificateCheck'.
At line:3 char:72
+ ... //XXXXXXXXX.XXXXX.XXXX/YYYYY/ZZZZZ/CCCCCC'}  -SkipCertificateCheck
+                                                     ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
1

There are 1 answers

0
Rafał Łątka On

-SkipCertificateCheck was added in powershell 6.0 You propably have powershell 5. You need to install newest version of powershell or try that solution

Powershell v3 Invoke-WebRequest HTTPS error