简体   繁体   English

将%*从批处理脚本作为$ args传递到Powershell脚本时转义参数

[英]Escaping arguments when passing %* from batch script as $args to powershell script

I have a batch script that takes any number of arguments (list of files) and executes a powershell script with the following command structure: 我有一个批处理脚本,它可以接收任意数量的参数(文件列表),并使用以下命令结构执行一个powershell脚本:

"%POWERSHELL%" -Command "%SCRIPT%" %*

%POWERSHELL% is the path to PowerShell.exe , and %SCRIPT% is my powershell script that interprets that receives %* as $args . %POWERSHELL%PowerShell.exe的路径, %SCRIPT%是我的powershell脚本,该脚本解释将%*接收为$args The problem is that if I pass in something like the filename test$file.name , PowerShell receives test.name , presumably because $file is interpreted as an empty variable. 问题是,如果我传入文件名test$file.name类的东西,PowerShell会收到test.name ,大概是因为$file被解释为空变量。

Is there a good way to escape each argument with single quotes or backticks from the batch script, or otherwise deal with this? 是否有一种好方法可以从批处理脚本中用单引号或反引号将每个参数转义,或者以其他方式处理?

Escape $ characters before you pass %* to the PowerShell script. 在将%*传递给PowerShell脚本之前,请转义$字符。

set ARGS=%*
set ARGS=%ARGS:$=`$%
"%POWERSHELL%" -Command "%SCRIPT%" %ARGS%

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

相关问题 从批处理脚本中调用Powershell命令时,双引号是否不能从字符串转义? - Double quotes not escaping from string when calling Powershell command from within Batch script? 通过传递参数从另一个PowerShell脚本调用一个PowerShell脚本 - Invoke one PowerShell script from Another PowerShell script with passing arguments 将参数从批处理文件传递给powershell脚本 - Passing parameters to powershell script from batch file 如何将 arguments 传递给从批处理脚本以管理员身份执行的 PowerShell 脚本? - How to pass arguments to PowerShell script executed as administrator from batch script? 将参数从Windows批处理脚本传递到Powershell脚本 - passing parameters from a windows batch script into a powershell script 将命令行参数从Powershell脚本传递到python脚本 - Passing command line arguments from powershell script to a python script 将参数从批处理脚本传递到PowerShell脚本失败 - Passing argument from batch script to PowerShell script fails 将 \\ in 参数传递给 powershell 脚本会导致意外转义 - passing \ in argument to powershell script causes unexpected escaping 在Powershell脚本中传递可选参数 - Passing optional arguments in Powershell script 将参数传递给从 powershell 5 调用的 powershell 7 脚本 - Passing arguments to powershell 7 script invoked form powershell 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM