Trying to issue a curl POST command in PowerShell - powershell

I have been able to use curl to issue a query and return a result or series of named parameters.
I want to issue a POST command on these series of named parameters, but the "multiple" is tripping me up.
I can issue the POST command on one of the parameters, but not on the entire series of them.
The command I'm using within powershell is
curl.exe -u username:password -X POST http://site.url:8042/modalities/MCTEST/store -d '["6a3eb7c4-a9d83950-24d36e94-5c20d248-0b5ce989","8c93b430-757278ab-21ab643c-c98aa03d-da14148e","b8f648fa-175de243-de76b1c0-2dc7551a-928b86a5","c865b966-f0c7d2c2-0a1114e0-80531305-31cd104e","aac7f73f-fcb922ef-b950c4c1-ee1d512e-e2aeb5ae"]'
If I issue the command on only one, it processes fine...obviously, I don't need quotes or brackets, which I think is what is tripping me up.
curl.exe -u username:password -X POST http://site.url:8042/modalities/MCTEST/store -d 6a3eb7c4-a9d83950-24d36e94-5c20d248-0b5ce989
I've tried subbing the double quotes with a backslash, tried adding a backslash, I've tried everything i can think of...I'm missing something.
I get a response of "must provide a json value"
I'm not sure what my error is in my syntax.
Any ideas?

external commands have to be called/quoted properly
PowerShell: Running Executables
Solve Problems with External Command Lines in PowerShell
Top 5 tips for running external commands in Powershell
Execution of External Commands in PowerShell Done Right
Using Windows PowerShell to run old command-line tools (and their
weirdest parameters)
See also about Redirection
Powershell: Pipe external command output to another external command
How to use the curl command in PowerShell?
Stackoverflow example by Ansgar Wiechers
$CurlArgument = '-u', 'xxx#gmail.com:yyyy',
'-X', 'POST',
'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',
'--data', 'content=success'
$CURLEXE = 'C:\Program Files\Git\mingw64\bin\curl.exe'
& $CURLEXE #CurlArgument

Related

PowerShell runs Invoke-WebRequest instead of curl [duplicate]

I'm new to cURL, just got it installed but it seems to only do what it feels like. I'm using the 64 bit version I got from here: http://curl.haxx.se/latest.cgi?curl=win64-ssl-sspi with installation instructions I found here: http://guides.instructure.com/m/4214/l/83393-how-do-i-install-and-use-curl-on-a-windows-machine. Opening a new Powershell window I'm able to use a simple GET request like so:
curl http://localhost:3000
but if I run a POST
curl -d "hello world" http://localhost:3000
it tells me "Invoke-WebRequest : Parameter cannot be processed because the parameter name 'd' is ambiguous. Possible matches include: -DisableKeepAlive -Debug."
Trying to get help I type
curl -h or curl --help
gives me "Invoke-WebRequest : Missing an argument for parameter 'Headers'. Specify a parameter of type 'System.Collections.IDictionary' and try again."
As I mentioned, I'm a cURL newbie but it seems strange that it can do get requests but nothing else. Any ideas what I'm doing wrong?
Windows 7 64 bit
Powershell version 4
Your problem is that your are not using the Curl you installed but a CmdLet called Invoke-WebRequest.
Just execute :
Remove-item alias:curl
And test your curl again, then store it in your profile.
The explanation is that it exists a native alias to the Invoke-WebRequest which is a CmdLet that is supposed to deliver a kind of curl service.
From Windows 10 build 17063 and later (April 2018), Curl is included into Windows, so that you can execute it directly from Cmd.exe or PowerShell.exe. To use it in PowerShell be careful to unalias this CmdLet or explicitly call curl.exe.
Built with Schannel (Microsoft's native TLS engine), libcurl still perform peer certificate verification, but instead of using a CA cert bundle, it uses the certificates that are built into the OS.
You can execute curl commands with Command Prompt instead of Windows Powershell. Command prompt doesn't alias curl commands like Windows Powershell does.
To open command prompt, hit Win + R, type cmd in the input box, <Enter>.

What is the PowerShell Equivilant of `curl -L https://unpkg.com/#pnpm/self-installer | node`?

I'm using GitLab.com with a Windows OS runner, so we commonly use PowerShell in place of Bash for scripting steps in the console. I'm trying to use pnpm and the examples they give suggest the following
$ curl -L https://unpkg.com/#pnpm/self-installer | node
$ install pnpm
What's the PowerShell version of the above curl requests piped into node?
I've been trying both Invoke-RestMethod and using the curl alias from within PowerShell, like
PS> curl https://unpkg.com/#pnpm/self-installer | node
but have been getting
SyntaxError: Invalid or unexpected token
Also from the curl docs, I know --location is needed.
As a best practice within scripts, I'd suggest explicitly using the parameter names and passing the correct type rather than letting the interpreter coerce for you (Uri takes a System.Uri and System is imported to PowerShell as a default):
$uri = [Uri]'https://unpkg.com/#pnpm/self-installer'
$js = Invoke-RestMethod -Uri $uri
The reason for your syntax error? the # symbol is special in PowerShell for a feature called Splatting.
When I ran the above, it spit out the contents of a js script to my console. It can then be assumed node takes cli, piped input and is in your PATH:
$js | node
Another assumption is you have an install in your PATH or the script creates some kind of interactive terminal:
$ install pnpm
--location isn't necessary here since Invoke-RestMethod will follow a redirect up to 5 hops by default (this can be configured with MaximumRedirection).

Curl Error: Parameter cannot be processed because the parameter name 'u' is ambiguous

I am trying to connect (in PowerShell) via
curl
but with no success.
Below is the following code I've inserting in order to establish the connection:
curl -u <USER>:<PASSWORD> https://something.com
but got the error:
Invoke-WebRequest : Parameter cannot be processed because the parameter name 'u' is ambiguous.
Possible matches include: -UseBasicParsing -Uri -UseDefaultCredentials -UserAgent.
So, I tried to look for a solution at SO, such as:
PowerShell's Invoke-RestMethod equivalent of curl -u (Basic Authentication)
Running cURL on 64 bit Windows
and on GitHub:
https://github.com/PowerShell/PowerShell/issues/4351
But got no success.
I also reinstalled the 'curl' in my machine and tried to use Invoke-WebRequest directly, but the problem persisted.
I'm new at PowerShell, maybe I'm doing a mistake when coding those lines, but do you have any suggestion how to deal with?
Do you think there is a better Command Prompt/CLI than PowerShell to use curl?
As #Maximilian Burszley wrote, curl in PowerShell is an alias to another command, not the curl command you intended.
In my opinion the simplest solution is to remove the alias, and then the curl command will be available without the overhead of Get-Alias and without using curl with the executable's path.
This is the command to remove the curl alias when using Windows PowerShell:
Remove-item alias:curl

Launch external program with multiple commands via Powershell

I am currently attempting to launch a different console (.exe) and pass multiple commands; while starting and entering a command works just fine, I haven't been able to find out how multiple ones can be entered via powershell.
& "C:\Program Files\Docker Toolbox\start.sh" docker-compose up -d --build
The given command works fine, but as mentioned I need to pass more than one command - I tried using arrays, ScriptBlocks and different sequences, though to no avail.
Edit:
Noticed that the docker build has a -f tag which allows me to specify a file; however, the issue now seems to be that the executed cmd removes all backslashes & special characters, rendering the path given useless.
Example:
&"C:\Program Files\Docker Toolbox\start.sh" 'docker-compose build -f
path\to\dockerfile'
will result in an error stating that "pathtodockerfile" is an invalid path.
Your start.sh needs to be able to handle multiple arguments. This doesn't look like a PowerShell question
Turns out that it was easier than expected; Solved it by executing a seperate file that contained the two commands needed and passing it to the start.sh file.
&"C:\Program Files\Docker Toolbox\start.sh" './xyz/fileContainingCommands.sh'

How to execute Powershell script on the fly

If I have a Powershell script accessible through HTTP, how can I download it and execute it on the fly like what we can do in the bash?
curl --user user:pass https://fake.url | bash
First of all, you shouldn't do that. I mean, downloading random code from the web and running it without taking a look at it? Yikes!
Yes, it's convenient, but it's also a horribly stupid idea, just as curl | bash, or even worse, curl | sudo bash is.
In most cases you can probably use WebClient.DownloadString and pass it to Invoke-Expression: Invoke-Expression (New-Object Net.WebClient).DownloadString(«url»). I'm not sure whether that still opens a script: scope for variables, though, so if that scope is used in the script it might not work. In that case you can download it as a file and execute the script. Then you also have to take care of the execution policy, though.
Try
curl --user user:pass https://fake.url | powershell -Command -
If the value of Command is "-", the command text is read from standard input.