【问题标题】:Caret not working in batch file, works in Command Prompt插入符号不在批处理文件中工作,在命令提示符下工作
【发布时间】: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.4MBPowerShell 32MB 相比。 (仅打开 powershell 就需要 80 倍的内存)当然,如果你有 4G 空闲——谁在乎......
  • 复制powershell.exe 只是为了运行一个命令(委婉地说)效率很低。如果您需要 PowerShell,请使用它并保持打开状态。 (那么您不会低效地创建新副本,因为它已经打开了。)
  • @Bill_Stewart 我从批处理文件运行它。从 cmd.exe 运行它只是为了测试它。

标签: powershell batch-file


【解决方案1】:

我这样做的方式效率低下。我正在结束这个问题。我正在这里处理不同版本的新脚本:Add Multiple URLs to this Powershell Script? 这是相关的。

【讨论】:

    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多