Community Pick: Many members of our community have endorsed this article.

Enable UserID and Password Prompt in VBScript

Published:
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealing with remote computer administration scripts.

Many of us who are working on VBScripts and automation tasks might have encountered a need for prompting the user to provide the login credentials (Username/Password), to perform some task during script execution. It's not always safe to include the logon credentials as part of the code itself because these files are in a plain text format and anyone can read it.

If you’re running Windows XP or Windows Server 2003, you can use ScriptPW (a COM object found only in those two versions of Windows) to mask passwords from the command line. Here’s a sample script that creates an instance of the ScriptPW.Password object and then uses the StdOut Write method to request that the user enter a password:

Set objPassword = CreateObject("ScriptPW.Password") 
                      WScript.StdOut.Write "Please enter your password:" 
                      
                      strPassword = objPassword.GetPassword() 
                      Wscript.Echo
                      Wscript.Echo "Your password is: " & strPassword

Open in new window


When you run this script, the message "Please enter your password:" appears on the screen. At that point, the script will pause and wait for you to type in a password; you simply type the password and press ENTER. This line of code will then grab the password and store it in the variable strPassword:

strPassword = objPassword.GetPassword()

Open in new window


For this simple script, we just echo back the password typed in to prove that the keystrokes really were captured. We do this because with the GetPassword() method, the keystrokes you type are not displayed onscreen. In other words, the password gets masked.

But this might not serve as a solution to our problem since there are a few drawbacks with this object.  To illustrate, if you try to run this script just by double-clicking the .vbs file (which includes this code) it won't work. To be able to run this code you need to go to a command prompt and run the script by giving the command CSCRIPT scriptPath\scriptFileName.vbs and this can be very inconvenient.  Another drawback is that it only works on two versions of Microsoft Windows.

By default in VBScript you can't find any functionality which could ask a user to provide the userID and password in a GUI environment.  Hence I found an article with code at the Microsoft Scripting Guy site to get these credentials through a web page. I have tweaked the code, which you can find below, so that one can easily use the code to add this functionality to their script without having to first write a html file to ask for the username and password. I am sharing this code as given below, and hope this will help someone who may need it:

You can save this code in a .vbs file to use it.
'=======================[ ASK Password ]========================================'
                      Option Explicit
                      Dim strUserID, strPassword
                      
                      AskPassword
                      
                      Sub AskPassword()
                      Dim htmlPwdCode, objCodeFile, objFileSysObj, objBrowser, strButton
                      Const FOR_WRITING = 2
                      
                      Set objFileSysObj = CreateObject("Scripting.FileSystemObject")
                      
                      htmlPwdCode = "<SCRIPT LANGUAGE=" & Chr(34) & "VBScript" & Chr(34) & ">" & Chr(13) & _
                      "Sub RunScript" & Chr(13) & _
                      "    OKClicked.Value = " & Chr(34) & "OK"& Chr(34) & Chr(13) & _
                      "End Sub" & Chr(13) & _
                      "Sub CancelScript" & Chr(13) & _
                      "    OKClicked.Value = " & Chr(34) & "Cancelled" & Chr(34) & Chr(13) & _
                      "End Sub" & Chr(13) & _
                      "Sub Default_Buttons" & Chr(13) & _
                      "	If Window.Event.KeyCode = 13 Then" & Chr(13) & _
                      "		btnOK.Click" & Chr(13) & _
                      "	End If" & Chr(13) & _
                      "End Sub" & Chr(13) & _
                      "</SCRIPT>" & Chr(13) & _
                      "<BODY onkeypress='vbs:Default_Buttons'><center><font size=" & Chr(34) & "2" & Chr(34) & " face=" & Chr(34) & "Arial" & Chr(34) & ">" & Chr(13) & _
                      "User name:&nbsp;&nbsp;&nbsp;" & Chr(13) & _
                      "<input type=" & Chr(34) & "text" & Chr(34) & " name=" & Chr(34) & "UserName" & Chr(34) & " size=" & Chr(34) & "30" & Chr(34) & "><br>" & Chr(13) & _
                      "Password :&nbsp;&nbsp;&nbsp; </font><font face=" & Chr(34) & "Arial" & Chr(34) & ">" & Chr(13) & _
                      "<input type=" & Chr(34) & "password" & Chr(34) & " name=" & Chr(34) & "UserPassword" & Chr(34) & _
                      " size=" & Chr(34) & "30" & Chr(34) & "></font></p>" & Chr(13) & _
                      "<input type=" & Chr(34) & "hidden" & Chr(34) & " name=" & Chr(34) & "OKClicked" & Chr(34) & " size = " & Chr(34) & "20" & Chr(34) & ">" & Chr(13) & _
                      "<input id=" & Chr(34) & "btnOK" & Chr(34) & " class=" & Chr(34) & "button" & Chr(34) & _
                      " type=" & Chr(34) & "button" & Chr(34) & " value=" & Chr(34) & " OK " & Chr(34) & _
                      " name=" & Chr(34) & "ok_button" & Chr(34) & " onClick=" & Chr(34) & "RunScript" & Chr(34) & ">" & Chr(13) & _
                      "<input id=" & Chr(34) & "btnCancel" & Chr(34) & " class=" & Chr(34) & "button" & Chr(34) & _
                      " type=" & Chr(34) & "button" & Chr(34) & " value=" & Chr(34) & "Cancel" & Chr(34) & _
                      " name=" & Chr(34) & "cancel_button" & Chr(34) & " onClick=" & Chr(34) & "CancelScript" & Chr(34) & "></center></BODY>"
                      
                      Set objCodeFile = objFileSysObj.CreateTextFile("AskPassword.html", True)
                      objCodeFile.Write htmlPwdCode
                      objCodeFile.Close
                      Set objCodeFile = Nothing
                      
                      Set objBrowser = CreateObject("InternetExplorer.Application")
                      
                      With objBrowser
                      	.Height = 170
                      	.Width = 400
                      	.Top = 200
                      	.Left = 300
                      	.StatusBar = True
                      	.Toolbar = False
                      	.Resizable = False
                      	.Navigate CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName) & "\AskPassword.html"
                      	.Visible = True
                      End With
                      
                      Do Until objBrowser.ReadyState = 4
                      'wait till page loads'
                      Loop
                      
                      Do While objBrowser.Document.Body.All.OKClicked.Value = ""
                          Wscript.Sleep 50                 
                      Loop 
                      
                      strUserID = objBrowser.Document.Body.All.UserName.Value
                      strPassword = objBrowser.Document.Body.All.UserPassword.Value
                      strButton = objBrowser.Document.Body.All.OKClicked.Value
                      
                      objBrowser.Quit
                      
                      If strButton = "Cancelled" Then
                      	MsgBox "Operation cancelled, script will now exit!"
                      	Wscript.Quit
                      Else
                      	'Credentials accepted for further processing
                      End If
                      objFileSysObj.DeleteFile "AskPassword.html", True
                      
                      Set objBrowser = Nothing
                      Set objFileSysObj = Nothing
                      End Sub
                      '=======================[ GOT Password ]========================================'

Open in new window


HOW TO USE THIS CODE:
1) Copy this code
2) and paste the same into notepad/your vbscript code
3) Put the name of the procedure AskPassword in the code where you want the authentication process to start
4) The values captured for username and password will be stored in global variable names strUserID and strPassword respectively. You can refer to the UserID and password captured using this procedure in the code, after the call to this AskPassword procedure

Please feel free to contact me in case you need any further clarification on how to use this code.

Regards,
SavindraSingh
3
24,851 Views

Comments (6)

Good solution! The only drawback is it doesn't work when save to the root drive (e.g. C:\)
aikimarkGet vaccinated; Social distance; Wear a mask
CERTIFIED EXPERT
Top Expert 2014

Commented:
Note to PEs: the title of this article has a typo (Propmt)
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
>> the title of this article has a typo

So it did.  Thanks aikimark.  Fixed.

Sungenwang, it should work from the root of a drive if you change this:
      .Navigate CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName) & "\AskPassword.html"

to this
      .Navigate Replace(CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName) & "\AskPassword.html", "\\", "\")


Regards,

Rob.
nice! will keep in my toolbox...

Commented:
Hi,

I am trying to use this script but when the browser window opens, the script exits.

'The interface is unknown' at line 59

Do Until objBrowser.ReadyState = 4

Can anyone help?

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.