我已经批量创建了一个程序,要求输入密码以继续执行程序的下一部分。我有一个我创建的文件,其中包含密码。在程序中我得到它来调用该文本文件的内容并将其设置为一个变量'密码'...唯一的问题是我收到一个错误说:'文件被使用在另一个过程'
这是我的代码中我找到错误的部分:
for /F "delims=" %%i in (Password.txt) do set content=%%i echo %content% set password123=%content%
powershell -Command $pword = read-host "Enter password" -AsSecureString ; ^ $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; ^ [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt
set /p password=<.tmp.txt & del .tmp.txt
if %password123%=%password% goto :correct
if not %password123%=%password% goto :incorrect
答案 0 :(得分:1)
绕过创建包含密码的临时文件。解析powershell
输出,例如如下:
for /F "delims=" %%G in ('
powershell -NoProfile -Command "$pword = read-host 'Enter password' -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"
') do (
set "password=%%G"
)
同时阅读整个 if /?
;使用适当的比较运算符==
。使用双引号可以转义可能的cmd
- 密码中的|<>&
等有毒字符
if "%password123%"=="%password%" ( goto :correct ) else goto :incorrect
要正确处理密码中可能的双引号,请按以下方式应用delayed expansion:
SETLOCAL EnableDelayedExpansion
if "!password123!"=="!password!" (
ENDLOCAL
goto :correct
) else (
ENDLOCAL
goto :incorrect
)