我在 powershell 中打开 powershell 脚本时,它按预期工作
Invoke-WebRequest -Uri host.com -Body "Param1=Value&Param2=Value&Param3=&Param4=Value" -Method POST
但是当通过 cmd 传递相同的命令时
powershell.exe Invoke-WebRequest -Uri host.com -Body "Param1=Value&Param2=Value&Param3=&Param4=Value" -Method POST
它给了我这些错误
'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.
我知道这是一个语法问题,但我不确定具体需要更改什么
编辑:
powershell.exe Invoke-WebRequest -Uri host.com -Body 'Param1=Value&Param2=Value&Param3=&Param4=Value' -Method POST
这一修改并没有改变结果
答案1
请注意,这些是来自命令,而不是 PowerShell。&
字符在未加引号或转义时将多个命令拆分为一行(就像;
在 PowerShell 中一样)。也许命令中的正文部分没有用双引号括起来,因为您实际上可能会运行:
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"
此外,您需要使用单引号或三引号引用主体数据"""
,因为powershell.exe
似乎删除了通过命令传递的脚本的双引号。
因此,最终的命令可能是这样的(-Command
在这种情况下,选项不会影响结果,但添加它可能有助于避免某些命令中的歧义):
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