[英]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.