0

The powershell script I have works as expected when opened in powershell

Invoke-WebRequest -Uri host.com -Body "Param1=Value&Param2=Value&Param3=&Param4=Value" -Method POST

but when the same command is passed via cmd as

powershell.exe Invoke-WebRequest -Uri host.com -Body "Param1=Value&Param2=Value&Param3=&Param4=Value" -Method POST

it gives me these errors

'Param1' is not recognized as an internal or external command,
operable program or batch file.
'Param2' is not recognized as an internal or external command,
operable program or batch file.
'Param3' is not recognized as an internal or external command,
operable program or batch file.
'Param4' is not recognized as an internal or external command,
operable program or batch file.

I know it's a syntax problem, but I'm not sure what needs to be changed exactly

EDIT:

powershell.exe Invoke-WebRequest -Uri host.com -Body 'Param1=Value&Param2=Value&Param3=&Param4=Value' -Method POST

That modification did nothing to change the results

Shrew
  • 3
  • 3
  • 3
    Does this answer your question? [run powershell command from cmd](https://superuser.com/questions/1080239/run-powershell-command-from-cmd) – squillman Sep 21 '22 at 14:14
  • Sadly, it didn't work – Shrew Sep 21 '22 at 14:24
  • Can you edit your question to include the updated command that you ran that still failed based on the duplicate question? – squillman Sep 21 '22 at 14:35
  • Follow the syntax guidance as documented from the PowerShell CLI itself. ***powershell /?*** – postanote Sep 21 '22 at 16:23
  • This works to paste into a cmd window " `powershell -command "Invoke-WebRequest -Uri host.com -Body 'Param1=Value&Param2=Value&Param3=&Param4=Value' -Method POST"` – Narzard Sep 21 '22 at 16:33
  • Shew, you said the pointer link did not work, but you are saying the line you posted did??? What you posted, syntactically, is the same as the pointer link. SO, is what you posted working or not? As per my ***powershell /?*** comment. Did you try the other examples that were provided via that? for example: ***powershell -Command "& {Invoke-WebRequest -Uri host.com -Body 'Param1=Value&Param2=Value&Param3=&Param4=Value' -Method POST}"*** – postanote Sep 21 '22 at 16:39
  • Ah, now I see what went wrong, I didn't use -command, Nazard's code did work, thanks again everyone. – Shrew Sep 21 '22 at 19:40

1 Answers1

0

Please note that these are errors from cmd, not PowerShell. & characters split multiple commands in one line when not quoted or escaped (just like ; in PowerShell). Maybe the body part is not wrapped in double quotation marks in your command, as you might actually run:

powershell Invoke-WebRequest -Uri host.com -Body 'Param1=Value&Param2=Value&Param3=&Param4=Value' -Method POST
powershell "Invoke-WebRequest -Uri host.com -Body "Param1=Value&Param2=Value&Param3=&Param4=Value" -Method POST"

Besides, you need to quote the body data using single quotation marks or triple quotes """, because powershell.exe seems to remove double quotation marks for script passed by commands.
Therefore, the final command could be like (-Command option does not affect the result in this case, but adding it may help avoid ambiguity in some commands):

powershell [-Command] "Invoke-WebRequest -Uri host.com -Body 'Param1=Value&Param2=Value&Param3=&Param4=Value' -Method POST"
powershell [-Command] Invoke-WebRequest -Uri host.com -Body """Param1=Value&Param2=Value&Param3=&Param4=Value""" -Method POST
powershell [-Command] Invoke-WebRequest -Uri host.com -Body 'Param1=Value^&Param2=Value^&Param3=^&Param4=Value' -Method POST
  • 1
    '@Wilderness Ranger': As for this ***e (-Command option is not necessary...):***. There are parameters/switches to virtually all things PS, and many are positional/many are not, so, would be assumed, if not provided. It does not mean they are not used by PS under the covers. Yet, as a best practice, for maintenance, readability, and troubleshooting, it's best to use the full verboseness of PS, unless you are doing ad-hoc, thor-away code, CLI stuff. Without being explicit, PS will try and help, but it's best not to make any system guess for you. Give it specifics for clarity. Yet, choices...;-} – postanote Sep 22 '22 at 17:52