Logon Scripts

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:

  • EnumNetworkDrives method, in addition to MapNetworkDrive
  • For.... Next Looping
  • If .... then... Else.... EndIf constructions

MapNetworkDrive Already Connected

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.

Example 1 - MapNetworkDrive with Drive Already Connected

Our objective is to map the W: to a share called '\drivers' on a server called '\\alan'.

Pre-requisites

  1. On Line 12 change the server name from '\\alan' to your server name.
  2. 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

  1. Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
  2. Change the server name from "\\alan to the name of your server.
  3. Save the file with .vbs extension e.g. Already.vbs.
  4. 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 ToolFree Permissions Analyzer for Active Directory

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!

Download Permissions Analyser - Free Active Directory Tool

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
$PSnet = $(New-Object -ComObject WScript.Network)
$PSnet.MapNetworkDrive("H:", "\\BigServer\Stuff")

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.

Summary - MapNetworkDrive Already Connected

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

Logon Script Home   • EnumNetworkDrives   • Map Network Drive Group   • Free Import CSV Tool

Map Network Drive Script   • Vbscript Map Network Drive Username   • Map Multiple Network Drives

ObjNetwork.MapNetworkDrive   • Disconnect Network Drives   • Logon script group policy

 

Download my Logon Script eBook for only $6.25

Logon ScriptThe 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.

  Jumbo Script 7 Package

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: