vbscript

Is there a way to hide/mask an input in vbs?


I am making a simple username and password storage program in vbs. After entering the username, I need to have a password input. I can't however, find a way to not show the input in plaintext and convert it to ****** This is what i have already:

x=MsgBox("VBScript username and password storage")
username = InputBox("Please enter your username", "Credentials storage", "Your username goes here")
passwd = InputBox("Please enter your password", "Credentials storage", "Your password goes here")

Set obj = CreateObject("Scripting.FileSystemObject")   
Const ForWriting = 2               
Set obj1 = obj.OpenTextFile("test.txt", ForWriting) 
obj1.WriteLine(username & " " & passwd)      
obj1.Close                                                                  
Set obj=Nothing   

I also tried doing this which I found as an answer on another question

Set oInput = CreateObject("ScriptPW.Password")
WScript.StdOut.Write "Enter password: "
pw = oInput.GetPassword

But when i ran it it said "ActiveX component can't create object "ScriptPW.Password"

Is there a way to hide the text in line 3 or fix my problem?


Solution

  • You can make a call to Powershell for a login window with a masked inputbox. There may be better solutions for this but this script is quite small and -importantly- does not require HTA/IE. If desired, you could pass the username, this example does not do that.

    Option Explicit
    
    Const WshRunning = 0
    Const WshFinished = 1
    Const WshFailed = 2
    Dim objShell, oExec, strOutput, strPS1Cmd
    
    'create Powershell command
    strPS1Cmd = "& { $cred = Get-Credential; Write-Output ('username: ' +$cred.Username + ' password in plain text: ' + $cred.GetNetworkCredential().Password) } "
    
    ' Create a shell and execute the powershell command
    Set objShell = WScript.CreateObject("wscript.shell")
    Set oExec = objShell.Exec("powershell -windowstyle hidden -command """ & strPS1Cmd & """ ")
    
    Do While oExec.Status = WshRunning
         WScript.Sleep 100
    Loop
    
    Select Case oExec.Status
       Case WshFinished
           strOutput = oExec.StdOut.ReadAll()
       Case WshFailed
           strOutput = oExec.StdErr.ReadAll()
     End Select
    
    WScript.Echo(strOutput)