简体   繁体   中英

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%" -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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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