profworkidea
profworkidea

Reputation: 11

curl to PowerShell command

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?

Upvotes: 1

Views: 7435

Answers (1)

Yash Gupta
Yash Gupta

Reputation: 2457

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"

Upvotes: 1

Related Questions