简体   繁体   English

"批量使用 PowerShell 命令的问题"

[英]Issue with using PowerShell command in batch

I download a zip file from my cloud using a PowerShell command.我使用 PowerShell 命令从我的云中下载了一个 zip 文件。 The command works correctly in PowerShell AND in the command line.该命令在 PowerShell 和命令行中正常工作。 However, if I insert the command from the command line into my batch script, only the html is downloaded.但是,如果我将命令行中的命令插入到批处理脚本中,则只会下载 html。 Why does the command work correctly in the command line but not in the batch file?为什么该命令在命令行中可以正常工作,但在批处理文件中却不能? Im out of ideas :D我没有想法:D

powershell Invoke-WebRequest """https://sync.luckycloud.de/d/fb56e4a8239a4c6cac7a/files/?p=%2FValheimServer%20Buddelkiste%20Modpack%20v3.4%20-%20Standart.zip&dl=1""" -OutFile """C:\Users\Anonymos\Downloads\servermodpack.zip"""

In a batch file - as opposed to the interactive cmd.exe command prompt [1] - you need to to escape % chars.批处理文件中 - 与交互式cmd.exe命令提示符[1]不同 -您需要转义%字符。 as %% in order to pass them through literally :作为%%以便从字面上传递它们

powershell -c "Invoke-WebRequest 'https://sync.luckycloud.de/d/fb56e4a8239a4c6cac7a/files/?p=%%2FValheimServer%%20Buddelkiste%%20Modpack%%20v3.4%%20-%%20Standart.zip&dl=1' -OutFile C:\Users\Anonymos\Downloads\servermodpack.zip"

Note:笔记:

  • I've used -c ( -Command ), the positionally implied Windows PowerShell CLI ( powershell.exe ) parameter explicitly for conceptual clarity (in PowerShell (Core) 7+ , whose CLI is pwsh , the default is now -f ( -File )).我使用了-c ( -Command ),位置隐含的Windows PowerShell CLI ( powershell.exe ) 参数,以明确概念(在PowerShell (Core) 7+中,其 CLI 是pwsh ,现在默认是-f-File ))。

  • Also, enclosing the entire command line to pass to PowerShell in "..." is generally preferable to prevent cmd.exe metacharacters other than % from causing problems.此外,通常最好将要传递给 PowerShell 的整个命令行包含在"..."中,以防止%以外cmd.exe元字符引起问题。

    • In case the command line requires use of embedded " characters, the safest way to escape them is to use "^"" (sic) with powershell.exe and "" with pwsh.exe - see this answer for details.如果命令行需要使用嵌入"字符,转义它们的最安全方法是将"^"" (sic) 与powershell.exe""pwsh.exe一起使用 - 有关详细信息,请参阅此答案

[1] In interactive use, % characters cannot technically be escaped , but they're retained as-is unless they're part of a cmd.exe -style environment-variable reference that refers to an existing environment variable, such as %OS% . [1] 在交互式使用中, %字符在技术上不能被转义,但它们会按原样保留,除非它们是cmd.exe样式的环境变量引用的一部分,该引用引用现有的环境变量,例如%OS% However, there are ways to treat % literally even in such cases, as discussed in this answer .但是,即使在这种情况下,也有一些方法可以按字面意思对待% ,如本答案中所述。 These techniques are important for invoking cmd.exe command lines programmatically from outside cmd.exe , such as from Node.Js or Python, because such programmatic invocations - perhaps surprisingly - use the rules of interactive cmd.exe sessions, not batch files.这些技术对于从cmd.exe外部(例如从 Node.Js 或 Python)以编程方式调用cmd.exe命令行非常重要,因为这种编程调用(也许令人惊讶)使用交互式cmd.exe会话的规则,而不是批处理文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM