1

Below is a curl script that gives the out in the json format (for details of snapshots):

curl -k -s -u superadmin:password-X GET https://127.0.0.1/snapshots -H "Accept: application/json" -H "Content-Type: application/json"

I'm new to Powershell. I'm not sure which one to use. Invoke-RestMethod or Invoke-WebRequest.

I executed the below command:

$cred = Get-Credential
Invoke-WebRequest -Uri 'https://127.0.0.1/snapshots' -Method GET -Credential $cred

And I get the below error:

Invoke-WebRequest : Unable to connect to the remote server At line:2 char:1 Invoke-WebRequest -Uri 'https://127.0.0.1/snapshots' -Method GET - ... CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Can you please help to convert it to Powershell?

1 Answer 1

1

Invoke-RestMethod is supposed to be the appropriate PowerShell equivalent for curl command.

So, your existing curl command:

curl -k -s -u superadmin:password-X GET https://127.0.0.1/snapshots -H "Accept: application/json" -H "Content-Type: application/json"

can be converted to PowerShell like this:

$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("superadmin:password"))}
Invoke-RestMethod -Method GET -Header $Header -ContentType "application/json" -uri "https://127.0.0.1/snapshots"
Sign up to request clarification or add additional context in comments.

9 Comments

Hi Yash, Thank you for your response. I get the following error: Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send. At line:1 char:1 + Invoke-RestMethod -Method GET -Header $Header -ContentType "applicati ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc eption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
It seems that you need to enforce TLS. Run this command & then try to run the above script: [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
use curl.exe and it works in powershell too...
Thank you Yash & Daniel. I tried with the option: [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 I still get the same error: Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send. At line:1 char:1 + Invoke-RestMethod -Method GET -Header $Header -ContentType "applicati ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
OS is Windows Server 2019 Standard edition. Poweshell Version : 5.1.17763.1971 I tried the following and get same error for both Invoke-RestMethod & Invoke-WebRequest. $cred = Get-Credential $Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("superadmin:password"))} Invoke-RestMethod -Method GET -Header $Header -ContentType "application/json" -uri "127.0.0.1/snapshots" -Credential $cred Invoke-WebRequest -Method GET -Header $Header -ContentType "application/json" -uri "127.0.0.1/snapshots" -Credential $cred
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.