如何在批量编程中用*******隐藏输入的密码?

时间:2016-03-31 06:42:03

标签: powershell batch-file

我有一个要求,比如每当我们执行test.bat文件时,它应该询问输入密码,输入的密码应该用*****隐藏。 对于上述要求,是否有任何最简单的批量编程方法?

我使用PowerShell编写了下面的脚本是否很好/建议使用PowerShell这个或任何其他最简单的方法?

@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=" %%G in (`%psCommand%`) do set password=%%G
echo "%password%"  
endlocal

还建议如何终止批处理文件 Ctrl + C 不起作用?

1 个答案:

答案 0 :(得分:0)

试试这个小例子:

@echo off
Title %~n0 by Hackoo 2016
Mode 50,5 & Color 0E
:CreatePassword
cls & Color 0E
setlocal DisableDelayedExpansion
Call :InputPassword "Please choose your password" pass1
Call :InputPassword "Please confirm your password" pass2
setlocal EnableDelayedExpansion
If !pass1!==!pass2! ( Goto:Good ) Else ( Goto:Bad )
::***********************************
:InputPassword
Cls
echo.
echo.
set "psCommand=powershell -Command "$pword = read-host '%1' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
      [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
        for /f "usebackq delims=" %%p in (`%psCommand%`) do set %2=%%p
Goto :eof
::***********************************        
:Good
Color 0B                  
Cls
echo.
echo                     Good password
::TimeOut /T 2 /NoBreak>nul
echo Your password stored as : "!Pass2!" without quotes
pause>nul
Goto :Eof
::***********************************
:Bad
Color 0C
Cls
echo.
echo             Wrong password try again
::TimeOut /T 2 /NoBreak>nul
echo  Press any key to retry again
pause>nul
Goto :CreatePassword
::***********************************

您还可以试试这个完整的示例 Folder Loker.bat

编辑于2016年3月31日@ 14:43 Another way in pur batch based on replace command

@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set /P "=Enter a Password:" < Nul
Call :PasswordInput
Echo(Your input was:!Line!
pause
Goto :Eof

:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" <Nul
Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" <Nul
If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd

这是 aGerman

改进的另一个版本
@echo off & setlocal
Title %~n0
Mode 50,5 & Color 9E
Set /P "Pass1=Please choose your Password:" < Nul
call :HInput Pass1
echo Input length is %errorlevel%
setlocal EnableDelayedExpansion
echo Your password is !Pass1!
pause
cls
Set /P "Pass2=Please confirm your Password:" < Nul
call :HInput Pass2
echo Input length is %errorlevel%
echo Your password is !Pass2!
pause
cls
If !Pass1!==!Pass2! (echo the two passwords are the same
) else (echo the two passwords does not match)
pause
goto :eof


:HInput [ByRef_VarName]
:: inspired by Carlos
if "%__HI__%" neq "__HI__" (
  setlocal DisableDelayedExpansion EnableExtensions
  set "CR=" &set "S=" &set "N=0" &set "__HI__=__HI__"
  for /f "skip=1" %%i in ('echo(^|replace ? . /u /w') do if not defined CR set "CR=%%i"
  for /f %%i in ('"prompt $H &for %%b in (1) do rem"') do set "BS=%%i"
)
set "C="
for /f skip^=1^ delims^=^ eol^= %%i in ('replace ? . /u /w') do if not defined C set "C=%%i"
setlocal EnableDelayedExpansion EnableExtensions
if "!CR!"=="!C!" (
  echo(
  if "%~1"=="" (
    echo(!S!
    endlocal &endlocal &exit /b %N%
  ) else (
    if defined S (
      for /f delims^=^ eol^= %%i in ("!S!") do endlocal &endlocal &set "%~1=%%i" &exit /b %N%
    ) else endlocal &endlocal &set "%~1=" &exit /b 0
  )
)
if "!BS!"=="!C!" (
  set "C="
  if defined S set /a "N -= 1" &set "S=!S:~,-1!" &<nul set /p "=%BS% %BS%"
) else set /a "N += 1" &<nul set /p "=*"
if not defined S (
  endlocal &set "N=%N%" &set "S=%C%"
) else for /f delims^=^ eol^= %%i in ("!S!") do endlocal &set "N=%N%" &set "S=%%i%C%"
goto HInput