[英]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>
请提出一些很好的方法来克服这种情况。
@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
@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.