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

僤鯓⒐⒋嵵緔 提交于 2019-12-07 07:44:37

问题


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%" -Command "%SCRIPT%" %*

%POWERSHELL% is the path to PowerShell.exe, and %SCRIPT% is my powershell script that interprets that receives %* as $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.

Is there a good way to escape each argument with single quotes or backticks from the batch script, or otherwise deal with this?


回答1:


Escape $ characters before you pass %* to the PowerShell script.

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


来源:https://stackoverflow.com/questions/12223790/escaping-arguments-when-passing-from-batch-script-as-args-to-powershell-scri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!