Blackkatt
Blackkatt

Reputation: 13

PowerShell -command from batch Invoke-RestMethod

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.

Upvotes: 1

Views: 7311

Answers (2)

xpost
xpost

Reputation: 1

For me I would do

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

Upvotes: 0

Guvante
Guvante

Reputation: 19223

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\`" }"

Upvotes: 2

Related Questions