VBScript Scripting Techniques > User Interaction > Passwords

Prompt for Passwords

 

ScriptPW.Password
VBScript Code:
strPw = Password( "Please enter your password:" )
WScript.Echo "Your password is: " & strPw

Function Password( myPrompt )
' This function hides a password while it is being typed.
' myPrompt is the text prompting the user to type a password.
' The function clears the prompt and returns the typed password.
' This code is based on Microsoft TechNet ScriptCenter "Mask Command Line Passwords"
' http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true

    ' Standard housekeeping
    Dim objPassword

    ' Use ScriptPW.dll by creating an object
    Set objPassword = CreateObject( "ScriptPW.Password" )

    ' Display the prompt text
    WScript.StdOut.Write myPrompt

    ' Return the typed password
    Password = objPassword.GetPassword()

    ' Clear prompt
    WScript.StdOut.Write String( Len( myPrompt ), Chr( 8 ) ) _
                       & Space( Len( myPrompt ) ) _
                       & String( Len( myPrompt ), Chr( 8 ) )
End Function
Requirements:
Windows version: XP, Server 2003, or Vista
Network: N/A
Client software: .NET Framework for Windows 98, ME, NT 4 & 2000
Script Engine: WSH, CSCRIPT.EXE only
Summarized: Works in Windows XP or later, only in CSCRIPT.EXE (uses StdOut).
Should also work in older Windows versions with .NET Framework installed (look for scriptpw.dll), only in CSCRIPT.EXE.
Doesn't work in Windows 95, nor in Windows 7.
 
[Back to the top of this page]
 
InternetExplorer.Application
VBScript Code:
Function GetPassword( myPrompt )
' This function uses Internet Explorer to
' create a dialog and prompt for a password.
'
' Version:             2.12
' Last modified:       2013-11-07
'
' Argument:   [string] prompt text, e.g. "Please enter password:"
' Returns:    [string] the password typed in the dialog screen
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Error handling code written by Denis St-Pierre
    Dim objIE
    ' Create an IE object
    Set objIE = CreateObject( "InternetExplorer.Application" )
    ' specify some of the IE window's settings
    objIE.Navigate "about:blank"
    objIE.Document.title = "Password " & String( 100, "." )
    objIE.ToolBar        = False
    objIE.Resizable      = False
    objIE.StatusBar      = False
    objIE.Width          = 320
    objIE.Height         = 180
    ' Center the dialog window on the screen
    With objIE.Document.parentWindow.screen
        objIE.Left = (.availWidth  - objIE.Width ) \ 2
        objIE.Top  = (.availHeight - objIE.Height) \ 2
    End With
    ' Wait till IE is ready
    Do While objIE.Busy
        WScript.Sleep 200
    Loop
    ' Insert the HTML code to prompt for a password
    objIE.Document.body.innerHTML = "<div align=""center""><p>" & myPrompt _
                                  & "</p><p><input type=""password"" size=""20"" " _
                                  & "id=""Password""></p><p><input type=" _
                                  & """hidden"" id=""OK"" name=""OK"" value=""0"">" _
                                  & "<input type=""submit"" value="" OK "" " _
                                  & "onclick=""VBScript:OK.value=1""></p></div>"
    ' Hide the scrollbars
    objIE.Document.body.style.overflow = "auto"
    ' Make the window visible
    objIE.Visible = True
    ' Set focus on password input field
    objIE.Document.all.Password.focus

    ' Wait till the OK button has been clicked
    On Error Resume Next
    Do While objIE.Document.all.OK.value = 0
        WScript.Sleep 200
        ' Error handling code by Denis St-Pierre
        If Err Then    'user clicked red X (or alt-F4) to close IE window
            IELogin = Array( "", "" )
            objIE.Quit
            Set objIE = Nothing
            Exit Function
        End if
    Loop
    On Error Goto 0

    ' Read the password from the dialog window
    GetPassword = objIE.Document.all.Password.value

    ' Close and release the object
    objIE.Quit
    Set objIE = Nothing
End Function
 
Sample VBScript Code:
strPw = GetPassword( "Please enter your password:" )
WScript.Echo "Your password is: " & strPw
 
Internet Explorer password dialog
 
Requirements:
Windows version: any
Network: any
Client software: Internet Explorer 4 or later
Script Engine: WSH (CSCRIPT and WSCRIPT)
(to use in HTAs, remove both WScript.Sleep lines)
Summarized: Works in all Windows versions with Internet Explorer 4 or later, remove both WScript.Sleep lines to use in HTAs.
 
[Back to the top of this page]

 

Fileaze banner

 


page last uploaded: 7 November 2013, 15:48
Fileaze