Last active
December 8, 2020 19:53
-
-
Save jalbam/a24edff03fee1241e98dce8c86f8968e to your computer and use it in GitHub Desktop.
Visual Basic Script (VBS) which asks for the password, being masked (not displaying typed characters), and stores it into a variable (working on Windows 10)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Visual Basic Script (VBS) which asks for the password, being masked (not displaying typed characters), and stores it into a variable (working on Windows 10). | |
' * Gist by Joan Alba Maldonado: https://gist.github.com/jalbam/a24edff03fee1241e98dce8c86f8968e | |
' Asks for the password: | |
Set WshShell = CreateObject("WScript.Shell") | |
powerShellCommand = "powershell -noprofile -command " & _ | |
"$passwordEncrypted = Read-Host -assecurestring ""Please, enter your password"";" & _ | |
"$passwordBinary = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwordEncrypted);" & _ | |
"$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($passwordBinary);" & _ | |
"write-output $password" | |
Set executor = WshShell.Exec(powerShellCommand) | |
executor.StdIn.Close | |
typedPassword = executor.StdOut.ReadAll | |
Call WshShell.Run("powershell -noprofile -command Remove-Variable -Name * -ErrorAction SilentlyContinue", 0, TRUE) | |
' Removes carriage return or new line invisible characters (if any): | |
If (Right(typedPassword, 2) = vbCrLf OR Right(typedPassword, 2) = vbNewLine) Then | |
typedPassword = Left(typedPassword, Len(typedPassword) - 2) | |
End If | |
' Shows the password typed: | |
' * NOTE: instead of doing this, do whatever you want with the password stored in 'typedPassword'. | |
WScript.Echo "[" & typedPassword & "]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment