我正在尝试在BATCH中制作的cmd程序中输入密码。我想知道是否有可能在BATCH中输入类似bash的密码。 ICYDK,我说的是当你在bash中键入密码时,它不会显示字符,而是整个字段只是空白。
我目前正在使用这个:
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
我只是想知道是否有可能。
答案 0 :(得分:2)
此解决方案由Batch File Command Hide Password
提供@echo off
echo Put your password :
Call :getPassword password
echo %password%
pause & exit
::------------------------------------------------------------------------------
:: Masks user input and returns the input as a variable.
:: Password-masking code based on http://www.dostips.com/forum/viewtopic.php?p=33538#p33538
::
:: Arguments: %1 - the variable to store the password in
:: %2 - the prompt to display when receiving input
::------------------------------------------------------------------------------
:getPassword
set "_password="
:: We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"
:: Prompt the user
set /p "=%~2" <nul
:keyLoop
:: Retrieve a keypress
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"
:: If No keypress (enter), then exit
:: If backspace, remove character from password and console
:: Otherwise, add a character to password and go ask for next one
if defined key (
if "%key%"=="%BS%" (
if defined _password (
set "_password=%_password:~0,-1%"
set /p "=!BS! !BS!"<nul
)
) else (
set "_password=%_password%%key%"
set /p "="<nul
)
goto :keyLoop
)
echo/
:: Return password to caller
set "%~1=%_password%"
goto :eof