Monday, May 06, 2013

Map Drive with Domain Password Masked (Windows XP)

'#############################################################
'Author: Scot Geer //
'Script Purpose: Script to map a drive
'Script Reason: Issue with end-users connecting to VPN that
'               cannot connect to an "already" mapped drive
'               on an active directory domain. The fix is
'               to manually map the drive (issue with cached
'               credentials?)
'Script Date: Aug 2006
'Script Updated: April 2008
'Script Actions: Prompts user for username/password, then
'                maps the drive to the domain share. It also
'                has error-handling built-in (checks for
'                network, bad password, etc.) and informs the
'                user as such.
'                *Observe the password is masked when the
'                user types it in*
'Script Requirements:
'         1. XP or above is required (thanks to
'            "ScriptPW.Password", it will *not* work
'            on Win2k or below). FYI, this does *not* work in Vista

'            or Windows 7 due to the lack of the scriptpw.dll file.
'            It *might* work if you copy over that file from an XP
'            machine
'         2. This script must be run in a command line
'            window (with the 'cscript' parameter):
'            "cscript scriptName.vbs"
'         3. To automate this script, create a batch
'            file with the line:
'            "start cmd /k cscript /nologo scriptName.vbs"
'            Batch file and vbs script should reside
'            in the same folder
'         4. Technician will have to edit parameters
'            "drvLetter", "ShareDir", & "domain" in
'            order for this script to work
'

'#############################################################
Option Explicit

Dim objShell,objNetwork,objPassword
Dim drvLetter,shareDir,domain,userName,userPswd
Dim driveAlreadyMapped,errorNOS
Dim error001,error002,error003

Set objShell = WScript.CreateObject("Wscript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objPassword = CreateObject("ScriptPW.Password")

driveAlreadyMapped = False
errorNOS = False

drvLetter = "S:"    'change to any drive letter desire
shareDir = "\\serverName\shareName"   'change to your server share
domain = "domainName\"            'keep the backslash

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WScript.StdOut.Write "Please enter your username:"
userName = WScript.StdIn.ReadLine
WScript.Echo

If userName = "" Then
    WScript.Echo
    Wscript.StdOut.WriteLine String(60, "#")
    Wscript.Echo "No Username Entered or User Cancelled"
    quitscript
End If

Wscript.StdOut.Write "Please enter your password:"
userPswd = objPassword.GetPassword()
Wscript.Echo
WScript.Echo

mapDrive drvLetter,shareDir,domain,userName,userPswd
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


'Maps drive w/username, pswd. Invokes error handler if drive is already mapped
'******************************************************************************
Sub mapDrive(drv,share,dom,usr,pswd)

    On Error Resume Next

    objNetwork.MapNetworkDrive drv, share, "false", dom & usr, pswd

    errorHandler_mapDrive

    If Not(driveAlreadyMapped) And Not(errorNOS) Then
        WScript.Echo
        Wscript.StdOut.WriteLine String(60, "#")
        Wscript.StdOut.WriteLine "Success!"
        WScript.Echo
        Wscript.StdOut.WriteLine "You may now open your ""Shared"" files"
        Wscript.StdOut.WriteLine String(60, "#")
    End If

End Sub
'*******************************************************************|

'Notifies user if drive is already mapped, bad username/pswd, or off network
'******************************************************************************
Sub errorHandler_mapDrive

    If Err.Number<>0 Then

        error001 = InStr(1,Err.Description,"already",1)
        error002 = InStr(1,Err.Description,"found",1)
        error003 = InStr(1,Err.Description,"password",1)

        If (error001) Then
            driveAlreadyMapped = True
            WScript.Echo
            Wscript.StdOut.WriteLine String(60, "#")
            Wscript.StdOut.WriteLine "Your ""Shared"" drive is already mapped"
            WScript.Echo
                WScript.StdOut.WriteLine "Open your ""Shared"" files as normal"
                WScript.Echo
                WScript.StdOut.WriteLine "Notice001"
                Wscript.StdOut.WriteLine String(60, "#")
        ElseIf (error002) Then
            errorNOS = True
            WScript.Echo
            Wscript.StdOut.WriteLine String(60, "#")
            Wscript.StdOut.WriteLine "The path to the drive could not be found"
            WScript.Echo
            WScript.StdOut.WriteLine "Drive = " & shareDir
            WScript.Echo
            Wscript.StdOut.WriteLine "Check your network connection to ensure " _
                & "you are connected to the Internet"
            WScript.Echo
            Wscript.StdOut.WriteLine "Make sure you are connected to the VPN"
            WScript.Echo
            Wscript.StdOut.WriteLine "Notice002"
            Wscript.StdOut.WriteLine String(60, "#")
        ElseIf (error003) Then
            errorNOS = True
            WScript.Echo
            Wscript.StdOut.WriteLine String(60, "#")
            Wscript.StdOut.WriteLine "Wrong username or password"
            WScript.Echo
            Wscript.StdOut.WriteLine "Run this script again"
            WScript.Echo
            WScript.StdOut.WriteLine "Ensure you are typing your username " _
                & "& password correctly"
            WScript.Echo
            WScript.StdOut.WriteLine "Check your CAPS LOCK"
            WScript.Echo
            Wscript.StdOut.WriteLine "Notice003"
            Wscript.StdOut.WriteLine String(60, "#")
        End If

          Err.Clear

    End If

End Sub
'*******************************************************************|

'Ends Script with a notification to the user
'******************************************************************************
Sub quitscript

    WScript.Echo
    WScript.Echo "Quitting Now"
    Wscript.StdOut.WriteLine String(60, "#")
    Wscript.Quit

End Sub
'*******************************************************************|