VBS Logon Script - Already Connected
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 Scenario
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:
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'.
Instructions to MapNetworkDrive
' Already.vbs Windows Logon Script
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.
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. Give this permissions monitor a try - it's free!
Use PowerShell for Logon Scripts
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:
# PowerShell Logon Script Example
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
See more logon scripts examples