Introduction to Map Network Drive - Already Connected
This page builds on the simple MapNetworkDrive script by first checking to see if the drive is already connected.
Normally, if you execute a MapNetworkDrive script for a second time, the script would fail. However, this script has built in logic and if the drive is already mapped, then script disconnects it. Alternatively, you could
view this script as a bit of fun, or as a chance to experiment with additional scripting techniques such as looping and if .. then ... else.. endif commands.
Topics for MapNetworkDrive - Already Connected
Already Connected ScenarioThe situation is that a user has already mapped their W:\ drive to a network share. Alternatively, you have already run the script once before. In
any event, you want your script to map to the W:\ drive. To
save an error, this script tests for the drive letter mapping, and if necessary, disconnects the W:\ drive before running the second half of the script. From a technical point of view, this is an advanced
MapNetworkDrive script because it introduces extra methods and commands:
- EnumNetworkDrives method, in addition to MapNetworkDrive
- For.... Next Looping
- If .... then... Else.... EndIf constructions
This is what I call a busy logon script. Busy because it has multiple parts and incorporates a variety of VBScript commands. It's almost impossible to build this script from scratch and get it
working first time. My advice is to get each section doing it's job, then bolt all the sections together.
Our objective is to map the W: to a share called '\drivers' on a server called '\\alan'.
Pre-requisites
- On Line 12 change the server name from '\\alan' to your server name.
- Make sure that your server has a share called '\drivers'. Or else change the reference to an actual share on your server.
Instructions to MapNetworkDrive
- Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
- Change the server name from "\\alan to the name of your server.
- Save the file with .vbs extension e.g. Already.vbs.
- Double click and check in your Windows Explorer for a new drive called :
drivers on 'alan' (W:)
' Already.vbs Windows Logon Script ' VBScript to map a network drive. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.7 - April 24th 2005 '
------------------------------------------------------' Option Explicit Dim strDriveLetter, strRemotePath Dim objNetwork, objShell Dim CheckDrive, AlreadyConnected, intDrive ' The section sets
the variables. strDriveLetter = "W:" strRemotePath = "\\alan\drivers"
' This sections creates two objects: ' objShell and objNetwork and counts the drives Set objShell =
CreateObject("WScript.Shell") Set objNetwork = CreateObject("WScript.Network") Set CheckDrive = objNetwork.EnumNetworkDrives()
' This section deals with a For ... Next loop ' See how it
compares the enumerated drive letters ' with strDriveLetter On Error Resume Next AlreadyConnected = False For intDrive = 0 To CheckDrive.Count - 1 Step 2 If CheckDrive.Item(intDrive)
=strDriveLetter _ Then AlreadyConnected =True Next
' This section uses the If = then, else logic ' This tests to see if the Drive is already mapped. ' If yes then disconnects If
AlreadyConnected = True then objNetwork.RemoveNetworkDrive strDriveLetter objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
' The first message box objShell.PopUp "Drive " &
strDriveLetter & _ "Disconnected, then connected successfully." Else objNetwork.MapNetworkDrive strDriveLetter, strRemotePath objShell.PopUp "Drive " & strDriveLetter & _ " connected
successfully." End if WScript.Quit
' Guy's Script ends here
VBS Learning Points
Note 1: You may wish to check on the EnumNetworkDrives method. Here we call this routine so that we can check each existing drive against the proposed drive value held in strDriveLetter.
Note 2: In order for our strategy to work, we need a loop. For ... Next. Note 3: During the loop we introduce the 'If' test. If we hit a drive that equals strDriveLetter then we
set the variable AlreadyConnected = true. Note 4: Armed with information about AlreadyConnected, if necessary, we RemoveNetworkDrive before mapping to the correct network path on the very next
line. Note 5: As I hinted earlier, there are many ways in VBScript to achieve the same effect. We could have just put in a plain and forceful RemoveNetworkDrive command at the beginning.
Another alternative would be to simply use On Error Resume Next to get around the situation where the drive is already mapped. However, I put it to you that none of these alternatives, would have been
so much fun, or provided so much learning as this script.
The strength of this page is the sheer variety of scripting commands, MapNetworkDrive, EnumNetworkDrives, RemoveNetworkDrive, not to mention the loops and If statements. When you get this logon script to
work, it will be a moment of true satisfaction.
See Also● Logon Script Home ●
MapNetworkDrive ● RemoveNetworkDrive ●
EnumNetworkDrives |