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

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