2016-12-04 48 views
0

我正在嘗試在BATCH中創建的cmd程序中獲取密碼輸入。我想知道是否可以在BATCH中使用類似bash的密碼輸入。 ICYDK,我說的是當你在bash中輸入密碼時,它不顯示字符,而是整個字段只是空白。如何在BATCH中輸入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 

我只是想知道,如果它是可能的。

+1

只要你有PowerShell和批次的混合。你的問題沒有用powershell標記,那麼你想要什麼語言? – Squashman

+4

[在批處理文件中隱藏輸入]的可能重複(http://stackoverflow.com/questions/5852759/hide-input-in-batch-file) – Squashman

回答

1

該解決方案是從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