2016-03-31 9 views
0

私は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が動作していないとして、バッチファイルを終了する方法をお勧め?

+0

によって改善別のバージョンである... – aschipfl

+3

ですから、バッチを呼び出すためにPSを使用しています?なぜPSで完全に仕事をしないのですか? – Martin

+0

このバッチファイルは、passwordをパラメータとして渡すことにより、内部的に他のバッチファイルを呼び出します。 – Aryan

答えて

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 
::*********************************** 

そして、あなたはまた、14:43 @この完全例えば31/03/2016にFolder Loker.bat

編集を試してみることができます

@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 

そして、これは私がPowerShellが戻っMS-DOSの時代に、すでに利用可能であった知らなかった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 
+0

set "psCommand = powershell -Command"を使用しました。$ pword =読み取りホスト '%1' -AsSecureString;^ $ BSTR = [System.Runtime.InteropServices.Marshal] :: SecureStringToBSTR($ pword);^ usebackq delims = "%ps(%psCommand%)"で%2 = %% pを設定すると、/ fのために のために[System.Runtime.InteropServices.Marshal] :: PtrToStringAuto($ BSTR)コードには、入力されたパスワードを最も単純な方法でマスクする他の最良の方法がありますか? – Aryan

+0

@Aryanあなたはそれを一括して行い、powershellでそれを使用したくないということを意味しましたか?それはあなたの目標ですか? – Hackoo

+0

はい、まさにバッチを使用して簡単に推薦することもできます。 – Aryan

関連する問題