Subscribe to Windows IT Pro
October 22, 2001 12:00 AM

Rem: Masking Passwords in Scripts

Windows IT Pro
InstantDoc ID #22712
Rating: (2)
Downloads
22712.zip

Do you have a scripting-related question or problem? You can send your question or problem to winscriptsol@win2000mag.com.

I need to create a script that prompts the operator for a password. I know that I can use VBScript's InputBox function or the WScript object's StdIn property in Windows Script Host (WSH) to prompt the user. However, both InputBox and StdIn display the password as the user types it. I prefer to mask the password. Does VBScript or WSH provide a way to mask passwords?

Neither VBScript nor WSH can mask passwords. However, Windows .NET Server (formerly code-named Whistler) and Windows XP include a new password component, scriptpw.dll, that provides this functionality. Script-pw.dll is a standard system component that resides in the \%system root%\ system 32 directory.

Listing 1 illustrates how you can use scriptpw.dll to retrieve a user's password without displaying the password as the user types it. The script begins by creating an instance of the Password object using VBScript's Create-Object function with the password component's programmatic identifier (ProgID), ScriptPW.Password.

Next, the script displays a prompt that tells the user to enter a password. After the user enters a password and presses Enter, the Password object's GetPassword method retrieves the password and returns the captured text as a string. The script stores the string in the strPassword variable. For the purpose of this example, the last line of the script echoes the value of strPassword. You wouldn't want to include this line in an actual script because it defeats the purpose of using scriptpw.dll to mask the password.

Because you can use the password component only at the command prompt, you must use the CScript host to run WSH scripts that call scriptpw.dll. Using the WScript host will result in an error.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    8 years ago
    Dec 28, 2004

    salut

  • hypert
    8 years ago
    Sep 16, 2004

    Well, the formatting of that code looks great, doesn't it?

    Let me try to force linebreaks in there:

    Sub Force(sScriptEng)

    ' Forces this script to be run under the desired scripting host.

    ' Valid sScriptEng arguments are "wscript" or "cscript".

    ' If you don't supply a valid name, Force will switch hosts.

    If Lcase(Right(Wscript.FullName, 12)) = "\\wscript.exe" Then

    'WScript.Echo "Running under WSCRIPT"

    If Instr(1, Wscript.FullName, sScriptEng, 1) = 0 Then

    'Need to switch to CSCRIPT

    CreateObject("Wscript.Shell").Run "cscript.exe " & Chr(34) & Wscript.ScriptFullName & Chr(34)

    Wscript.Quit

    End If

    Else

    'WScript.Echo "Running under CSCRIPT"

    If Instr(1, Wscript.FullName, sScriptEng, 1) = 0 Then

    'Need to switch to WSCRIPT

    CreateObject("Wscript.Shell").Run "wscript.exe " & Wscript.ScriptFullName

    Wscript.Quit

    End If

    End If

    end sub

    Force "cscript"

  • hypert
    8 years ago
    Sep 16, 2004

    To Andrew:

    You need to force your script to run under CScript. I found this online, and it works for me:

    Sub Force(sScriptEng)
    ' Forces this script to be run under the desired scripting host.
    ' Valid sScriptEng arguments are "wscript" or "cscript".
    ' If you don't supply a valid name, Force will switch hosts.

    If Lcase(Right(Wscript.FullName, 12)) = "\\wscript.exe" Then
    'WScript.Echo "Running under WSCRIPT"
    If Instr(1, Wscript.FullName, sScriptEng, 1) = 0 Then
    'Need to switch to CSCRIPT
    CreateObject("Wscript.Shell").Run "cscript.exe " & Chr(34) & Wscript.ScriptFullName & Chr(34)
    Wscript.Quit
    End If
    Else
    'WScript.Echo "Running under CSCRIPT"
    If Instr(1, Wscript.FullName, sScriptEng, 1) = 0 Then
    'Need to switch to WSCRIPT
    CreateObject("Wscript.Shell").Run "wscript.exe " & Wscript.ScriptFullName
    Wscript.Quit
    End If
    End If
    end sub

    Force "csript"

  • Ryan
    8 years ago
    Mar 05, 2004

    Where can you find that .dll? I searched my XP Pro CD-ROM and it does not appear. Am I missing something here?

  • Andrew Good
    9 years ago
    Sep 01, 2003

    While this works for the CScript host, what I'm looking for is something similar that I can run under the WScript host. There must be something suitable somewhere!

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.