简体   繁体   English

当我们传递用“(双反转逗号)引用的参数时,批处理脚本问题

[英]Issue with batch script when we pass parameter quoted with " (double inverted comma)

I have written below scripts which will ask user to input password as parameter if I enter password "Hello&123" it fails but it is working for other case like !Hello&123> please suggest some good approach to overcome this situation. 我写了下面的脚本,要求用户输入密码作为参数,如果我输入密码"Hello&123"它失败但是它适用于其他情况如下!Hello&123>请提出一些很好的方法来克服这种情况。

TestScript1.bat TestScript1.bat

@echo off
setlocal

set "psCommand=powershell -Command "$pword = read-host 'Enter password:' -AsSecureString ; ^
      $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
            [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
if "%password%" == "" @echo password cannot be empty & goto DIE
TestScript2.bat.bat testuser "192.168.1.1" "%password%"
endlocal

:DIE
exit /b 1

TestScript2.bat TestScript2.bat

@echo off

echo n | psftp %1@%2 -pw %3 -b Test3.bat >> TestLog.log 2>&1

You need to use delayed expansion when you try to expand the variable. 尝试扩展变量时,需要使用延迟扩展。
Delayed expansion is always safe for any content. 对于任何内容,延迟扩展始终是安全的。
But with delayed expansion there are problems with exclamation marks and carets. 但由于延迟扩张,感叹号和插入符号存在问题。
That's why you should enabled it after the set "password=%%p" . 这就是为什么你应该在set "password=%%p"之后启用它。

@echo off
setlocal DisableDelayedExpansion
set "psCommand=powershell -Command "$pword = read-host 'Enter password:' -AsSecureString ; ^
      $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
            $str=[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR); ^
            Write-host('#'+$str)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set "password=%%p"
setlocal EnableDelayedExpansion
set "password=!password:*#=!"
if not defined password (

    echo password cannot be empty
    goto :DIE
)
echo It works: pwd=!password!
TestScript2.bat testuser "192.168.1.1" "!password!"

But you will get the next problem with TestScript2.bat, as the expansion itself is safe, but you can't transfer an arbitrary password to a function or batch file by value (it's really impossible). 但是你会得到TestScript2.bat的下一个问题,因为扩展本身是安全的,但是你不能通过值将任意密码传递给函数或批处理文件(这实际上是不可能的)。

So you should transfer it by ref instead by value. 所以你应该通过ref而不是值来传递它。

TestScript2.bat testuser "192.168.1.1" password

And use it in TestScript2.bat again with !%~3! 并使用!%~3! 〜3再次在TestScript2.bat中使用它!%~3!

But to call the psftp command with the password causes again the problem with arbitrary values. 但是使用密码调用psftp命令会再次导致任意值的问题。
You need to escape the content to a safe format, in this case you have to regard the rules of psftp. 您需要将内容转义为安全格式,在这种情况下,您必须考虑psftp的规则。
I tested, psftp replaces \\"" to " . 我测试过,psftp将\\""替换为"

TestScript2.bat TestScript2.bat

@echo off
setlocal EnableDelayedExpansion
set "password=!%~3!"
set "password=!password:"=\""!"
... more replace rules
echo n | psftp %1@%2 -pw "!password!" -b Test3.bat >> TestLog.log 2>&1

I think what you want is something like this: 我想你想要的是这样的:

SET Test="Hello"
FOR /F "TOKENS=2 DELIMS==" %a in ('SET Test') DO @ECHO %~a

The %~a strips any quotes off. %~a删除任何引号。

The ampersand and the > would both need to be escaped ( !Hello^&123^> ) as they currently are not %3 is received as !Hello . &符号和>都需要转义( !Hello^&123^> )因为它们当前不是%3被收到!Hello

You can use delayed expansion to get around this: 您可以使用延迟扩展来解决此问题:

setlocal enableDelayedExpansion
TestScript2.bat testuser "192.168.1.1" !password!

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

相关问题 FOR / F在批处理文件中不适用于逗号分隔的双引号字符串 - FOR /F is not working for Comma separated double quoted string in batch file 将参数从批处理文件传递到MYSQL脚本 - Pass parameter from Batch file to MYSQL script 如何通过Powershell启动过程将带引号的参数传递给提升的批处理脚本 - How to pass quoted arguments to elevated batch script via powershell Start-Process 如何将带有双引号的字符串作为命令行参数传递到批处理文件 - How to pass string with double quotes as command line parameter to a batch file 如何将逗号分隔的参数传递到批处理文件,并在批处理文件中使用其他参数? - how to pass comma separated parameter to batch file and use if else in batch file? 双引号和单引号会在Windows批处理脚本中产生问题以终止进程 - Double and single quote creates issue in Windows batch script to kill process 批处理脚本:将返回的 GUID 作为桌面快捷方式的 URL 参数传递 - Batch script: Pass returned GUID as URL parameter for desktop shortcut 管理员使用双引号参数批处理文件的快捷方式 - Administrator's shortcut to batch file with double quoted parameters Git Bash 脚本回显命令 output 反参数后 - Git Bash script echo command output inverted after parameter 将字符串作为参数发送到批处理脚本中时,^ 会加倍 - The ^ gets doubled when sending string as parameter to function in batch script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM