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

7
  • 3
    Does this answer your question? run powershell command from cmd
    – squillman
    Commented Sep 21, 2022 at 14:14
  • Sadly, it didn't work
    – Shrew
    Commented Sep 21, 2022 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
    Commented Sep 21, 2022 at 14:35
  • Follow the syntax guidance as documented from the PowerShell CLI itself. powershell /?
    – postanote
    Commented Sep 21, 2022 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
    Commented Sep 21, 2022 at 16:33

1 Answer 1

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
  • 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
    Commented Sep 22, 2022 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .