Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running curl via powershell - how to construct arguments?

Tags:

powershell

I'm trying to run curl to upload a file in my script, using batch was painful because I need to do string manipulation etc so I turned to powershell.

However I can't seem to get powershell to execute Curl:

$hash = "test"
$fileToUpload = "hello world.txt"
$user = "user"
$password = "passy"
curl --ftp-create-dirs -T $fileToUpload -u ${user}:${pass} ftp://example.com/$hash/$fileToUpload

This results in:

Invoke-WebRequest : Parameter cannot be processed because the parameter name 'T' is ambiguous. Possible matches include: 
-TimeoutSec -TransferEncoding.
At line:5 char:24
+ curl --ftp-create-dirs -T $fileToUpload -u ${user}:${pass} ftp://example.com/$ha ...
+                        ~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Curl.exe is in my PATH.

like image 475
paulm Avatar asked Jun 12 '15 15:06

paulm


People also ask

How do you write curl command in PowerShell?

The conclusion is that if you need to use the curl (as same as in the Windows command prompt) in PowerShell then you need to call the curl executable( curl.exe ) directly. Else, you should stick to the PowerShell curl alias which resolves to the Invoke-WebRequest cmdlet under the hood.

Can you run curl in PowerShell?

curl requires minimal user interaction and is therefore preferred for automation. curl in PowerShell uses Invoke-WebRequest . From PowerShell 3.0 and above, you can use Invoke-WebRequest , which is equivalent to curl .

How do I run a curl script in Windows?

Invoke curl.exe from a command window (in Windows, click Start > Run and then enter "cmd" in the Run dialog box). You can enter curl --help to see a list of cURL commands.

What is curl command?

What Is the curl Command? curl (short for "Client URL") is a command line tool that enables data transfer over various network protocols. It communicates with a web or application server by specifying a relevant URL and the data that need to be sent or received.


1 Answers

In PowerShell curl is a built in alias to Invoke-WebRequest cmdlet. And aliases have priority in command resolution. To solve your problem you have more specifically, use curl.exe instead of curl, so command not resolved to alias. Or you can remove alias Remove-Item alias:curl, but as it is build in alias you have to put this command in your profile, or invoke it in every session.

If you are not sure how PowerShell resolve your command, then you can use Get-Command cmdlet:

Get-Command curl
Get-Command curl.exe
like image 130
user4003407 Avatar answered Sep 18 '22 15:09

user4003407