1

I've tried every combination I can think of, but the best I can come up with is an encoding issue. I know how to work around this by using -EncodedCommand or creating a small .ps1 script, but that's not what am looking for a couple of reasons.

So I hope, someone here knows what's going on here.

This works at PowerShell command prompt:

Invoke-RestMethod -Uri "http://127.0.0.1:8989/api/series" -Method Get -Header @{"X-Api-Key" = "1234"}

But when I try to pass it from command prompt or batch it fails!

powershell.exe -command "& {Invoke-RestMethod -Uri "http://127.0.0.1:8989/api/series" -Method Get -Header @{ "X-Api-Key" = "1234" } }"

powershell.exe -command "& Invoke-RestMethod -Uri "http://127.0.0.1:8989/api/series" -Method Get -Header @{ "X-Api-Key" = "1234" }"

powershell.exe -command Invoke-RestMethod -Uri http://127.0.0.1:8989/api/series -Method Get -Header @{ "X-Api-Key" = 1234 }

powershell.exe -command @{Invoke-RestMethod -Uri http://127.0.0.1:8989/api/series -Method Get -Header "X-Api-Key" = 1234 }

and many, many, many more combinations ;(

update 160614 removed all the error examples, not needed anymore.

1
  • Look at this. Commented Jun 13, 2016 at 19:07

2 Answers 2

2

You need to quote the entire thing and escape the quotes:

# If running within cmd
powershell.exe -command "& Invoke-RestMethod -Uri \"http://127.0.0.1:8989/api/series\" -Method Get -Header @{ \"X-Api-Key\" = \"1234\" }"

# If running within powershell
powershell.exe -command "& Invoke-RestMethod -Uri \`"http://127.0.0.1:8989/api/series\`" -Method Get -Header @{ \`"X-Api-Key\`" = \`"1234\`" }"
Sign up to request clarification or add additional context in comments.

5 Comments

PowerShell.exe command line parser actually use backslash for escaping not backtick.
@PetSerAl: Actually your shell escapes the quotes. Need to update. If you run within cmd.exe you need to use \
To run from PowerShell it need to be \`". PowerShell.exe command line parser need to see \ in the command line.
@PetSerAl: powershell.exe somehow transforms powershell -command "& echo ``"Hello world``"" to hello\nworld being outputted (hello world over two lines). Updated the post as you described.
Thank you so much! as soon as I say the "\" I remember I use that for findstr to search on multiply lines.
0

For me I would do

powershell -Command "Invoke-Webrequest https://file.com -Outfile file.file" & cls
pause >nul 

Comments

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.