【发布时间】:2017-07-03 19:43:39
【问题描述】:
我使用这个脚本来获取一个 url 的结果:
@Echo off
For /f "delims=" %%A in (
'powershell -NonI -NoP -C "(Invoke-Webrequest "%~1").content"'
) Do set Line=%%A
我可以像这样在命令提示符窗口中调用它:
WebResponse.cmd "http://websiteremoved.com/teststat.php?username^=%friend1%"
我需要使用插入符号 (^) 来取消等号。但是,当我在批处理文件中运行同一行代码时,它不起作用。根据测试,%~1 设置为"http://websiteremoved.com/teststat.php?username^=Lukaka",但powershell 部分返回:
Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'Lukaka'. [] http://websiteremoved.com/teststat.php?username^=Lukaka
At line:1 char:2 [] http://websiteremoved.com/teststat.php?username^=Lukaka
+ (Invoke-Webrequest http://websiteremoved.com/teststat.p ... [] http://websiteremoved.com/teststat.php?username^=Lukaka
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [] http://websiteremoved.com/teststat.php?username^=Lukaka
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException [] http://websiteremoved.com/teststat.php?username^=Lukaka
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand [] http://websiteremoved.com/teststat.php?username^=Lukaka
关于它为什么这样做的任何想法?非常感谢!
【问题讨论】:
-
你上次误会了,我说要么引用 要么 转义,而不是两者兼而有之。
-
为什么要使用
cmd.exe?只需从 PowerShell 提示符直接运行您想要运行的命令。 -
取决于您有多少可用内存。
cmd.exe 0.4MB与PowerShell 32MB相比。 (仅打开 powershell 就需要 80 倍的内存)当然,如果你有 4G 空闲——谁在乎...... -
复制
powershell.exe只是为了运行一个命令(委婉地说)效率很低。如果您需要 PowerShell,请使用它并保持打开状态。 (那么您不会低效地创建新副本,因为它已经打开了。) -
@Bill_Stewart 我从批处理文件运行它。从 cmd.exe 运行它只是为了测试它。
标签: powershell batch-file