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.
The 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
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 2010 '
------------------------------------------------------' 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.
Guy
Recommends: Permissions Analyzer - Free Active Directory Tool
I like the
Permissions Monitor because it enables me to see quickly WHO has permissions
to do WHAT. When you launch this tool it analyzes a users effective NTFS
permissions for a specific file or folder, takes into account network share
access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free utility saves when you are
troubleshooting authorization problems for users access to a resource.
VBScripts are being superseded by PowerShell .ps1 files.
While PowerShell is used mainly for configuring the operating system, it's
possible to its cmdlets to MapNetworkDrive. The technique is to create a
ComObject, which can act as a wrapper for familiar VBScript commands. Here is
example of PowerShell's New-Object cmdlet manipulating MapNetworkDrive:
You could save these instructions in a .ps1 file. However, the
hard part is executing this .ps1 file as a logon script. See
more about
assigning PowerShell logon scripts.
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.
If you like this page then please share it with your friends
The extra features you get in your eBook
include, more pages full of detailed examples. Also, ten 'how to...'
sections, with screen shots showing which menus to use. Go for Guy's
eBook - and get a printable version with copy enabled and no expiry date.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.