What would be the Windows batch equivalent for HTML's input type=“password”?

前端 未结 12 2003
有刺的猬
有刺的猬 2020-12-01 18:51

I need to get authentication credentials from the users within a Windows script but the classic \"first Google result\" approach:

SET /P USR=Username: 
SET /         


        
相关标签:
12条回答
  • 2020-12-01 19:24

    ConSet is a free tool written by Frank P. Westlake. It is an extended version of standard Windows command set.

    ConSet.exe - Displays, sets, or deletes cmd.exe environment variables, modifies console parameters, and performs floating point mathematics.

    As it is not a standard Windows console application, the usage of this tool requires either the distribution of this tool together with the batch file or the tool is stored on a server share and the batch file calls this tool directly from the server share.

    ConSet makes a prompt for a password string with hidden input assigned to an environment variable very easy:

    ConSet.exe /PH "PWD=Password: "
    

    The additional parameter H results in hiding user input.

    0 讨论(0)
  • 2020-12-01 19:25

    I assume that you want no echo of the password on the screen.

    If a pop-up window is ok for you, you could use e.g. VBScript to show an IE window displaying a password field. Here's an example.

    As an alternative you could call your script from an HTA (HTML Application) file (see Introduction to HTML Applications (HTAs).

    Regards, divo

    0 讨论(0)
  • 2020-12-01 19:27

    another alternative is my EditV32 (x86) or EditV64 (x64) command-line tools. For example:

    editv32 -m -p "Password: " PWD
    

    -m means "masked input" and -p is the prompt. The user's input is stored in the PWD environment variable. You can get it here:

    http://www.westmesatech.com/editv.html

    0 讨论(0)
  • 2020-12-01 19:29

    Another approach is to call PowerShell commands from your Batch script. Here's an example that configures the logon account of a service:

    $password = Read-Host "Enter password" -AsSecureString;
    $decodedpassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password));
    & "sc.exe" config THE_SERVICE_NAME obj= THE_ACCOUNT password= $decodedPassword;
    

    where THE_SERVICE_NAME is the name of the service to configure and THE_ACCOUNT is the logon account.

    Then we can use it from a batch script like that:

    call powershell -Command "$password = Read-Host "Enter password" -AsSecureString; $decodedpassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)); & "sc.exe" config THE_SERVICE_NAME obj= THE_ACCOUNT password= $decodedPassword;"
    

    which is simply calling PowerShell.exe and passing the three commands.

    The advantage of this approach is that the majority of Windows installations today include PowerShell, so no extra program or script is needed. The drawback is that you will need to either use the password inside the PowerShell call (like in my example) or store it in an environment variable and then use it from your batch script. I preffer the former because it is more secure and simpler.

    0 讨论(0)
  • 2020-12-01 19:32

    You may use ReadFormattedLine subroutine for all kind of formatted input. For example, the commands below read an username and password of 8 characters each, display asterisks in the screen, and continue automatically with no need to press Enter:

    call :ReadFormattedLine USR="********" /M "Username: "
    call :ReadFormattedLine PWD="********" /M "Password: "
    

    Or in a different way:

    call :ReadFormattedLine nameAndPass="******** / ********" /M "Enter Username / Password: "
    

    In previous example, when the user completed the username, the subroutine display the slash and read the password; if the user delete characters, the slash is also deleted automatically.

    This subroutine is written in pure Batch so it does not require any additional program, and it allows several formatted input operations, like read just numbers, convert letters to uppercase, etc. You may download ReadFormattedLine subroutine from Read a line with specific format.

    0 讨论(0)
  • 2020-12-01 19:33

    Another alternative is ReadLine.exe. Example:

    @echo off
    setlocal enableextensions
    set PASSWORD=
    for /f "delims=" %%p in ('readline -h -p "Enter password: "') do set PASSWORD=%%p
    echo You entered: %PASSWORD%
    endlocal
    
    0 讨论(0)
提交回复
热议问题