Logon Scripts

Windows Logon Scripts - EnumNetworkDrives

Introduction to EnumNetworkDrives

EnumNetworkDrives is one of the more advanced Windows logon script methods.  Therefore, I recommend that you master MapNetworkDrive before you try the Enum family of VBScripts.  My reasoning is this; firstly, you need to create a Network Drive, or two, before you can start enumerating.  Secondly, practicing on basic scripts will give the knowledge to make sense of my EnumNetworkDrives script.

Topics for EnumNetworkDrives

 ♦

Enumerate Mapped Network Drives Scenario

EnumNetworkDrives will open up new avenues for your scripts.  In particular, enumerating creates error-correcting opportunities in MapNetworkDrive or RemoveNetworkDrive scripts.

Let us suppose that you want to build extra 'If ... then.. else' logic into your Windows logon script.   Without EnumNetworkDrives, it would be difficult to get a handle on the drive letters.  However, with EnumNetworkDrives it's easy to for the script to list the drives that already exist. 

Here is an example of the logic that EnumNetworkDrives facilitates.  The situation is that you want to test that drive letter X is available, before you map this letter to a server's UNC path.  This is the error correcting code that you could build into the script:
If strFreeLetter = X, then map drive letter X to UNC path
Else map to drive letter Y to UNC path.

Although the above logic is straightforward in human terms, in fact it represents a complex task for VBScript.  Indeed, I would break the task down into a number of steps.  Once all the sections are working, I would join them into our production script.  Our task on this page is merely to understand the EnumNetworkDrives role in error correcting logic.

˚

EnumNetworkDrives Syntax

If you only remember one fact about EnumNetworkDrives, remember that it's Drives plural and not drive.  The syntax is deceptively simple:

colDrives = objNetwork.EnumNetworkDrives

However, to put the logon script to useful work, you need extra code, which actually displays the enumerated drives.  In real life you would then play those 'If .. then endif' games.  For now, we will concentrate on just the EnumNetworkDrives method.

For intDrive = 0 to colDrives.Count -1 Step 2
intNetLetter = IntNetLetter +1
WScript.Echo "UNC Path " & colDrives.Item(intDrive) & " = " _
& colDrives.Item(intDrive +1) & " Mapped drive No : " & intDrive
Next
 

 

Technically, EnumNetworkDrives outputs a collection of paired items.  The even numbers of each pair are the logical drives and the odd numbers represent the UNC paths.  Think of the collection as an array.  Imagine the logical drive letter in one column and the UNC path in the other.   By all means experiment with different values for Step 2, but I find two to be the best number, after all, these are paired items. Drive Letter : UNC Path.

Example - EnumNetworkDrives

EnumNetworkDrives is a difficult logon script method.  To give the script the best chance of working, we need to create a mapped network drive, otherwise there would be nothing for the script to enumerate.

Preparation - Create a mapped network drive

The only purpose of this script, is to create a mapped drive so that we can study EnumNetworkDrives.

Pre-requisites

  1. On Line 10, change the server name from '\\alan' to your server name.
  2. Make sure that your server has a share called '\home'.  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:)


'

' AddDrive.vbs - ' Windows Logon Script
' VBScript - Add drive to Practice RemoveNetworkDrive
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - April 24th 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork, objShell
Dim strDriveLetter, strNetworkPath, strExplorer
strDriveLetter = "H:"
strNetworkPath = "\\alan\home"

Set objNetwork = CreateObject("WScript.Network")
' Section which removes strDriveLetter
objNetwork.MapNetworkDrive strDriveLetter, strNetworkPath
Set objShell = CreateObject("WScript.Shell")
strExplorer = "Explorer" & " " & strDriveLetter
objShell.run (strExplorer)

' End of Script
 

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

Here is the main EnumNetworkDrives Script

Instructions to EnumNetworkDrives

  1. Copy and paste the script below into notepad.
  2. Save the file with .vbs extension e.g. EnumNetworkDrives.vbs.
  3. Double click and count your mapped network drives.


'

' EnumNetworkDrives.vbs - Windows Logon Script
' VBScript to Enumerate Network Drives
'.N.B. This script need mapped drives
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.8 - April 24th 2005
' -----------------------------------------------------------'
Option Explicit
Dim objNetwork, colDrives, intDrive, intNetLetter

' This is the heart of the script
' Here is where colDrives enumerates the mapped drives
Set objNetwork = CreateObject("WScript.Network")
Set colDrives = objNetwork.EnumNetworkDrives

' Extra section to troubleshoot
If colDrives.Count = 0 Then
WScript.Echo "Guy's warning: No Drives Mapped "
Wscript.Quit(0)
End If

' Here is the where the script reads the array
For intDrive = 0 To (colDrives.Count -1) Step 2
intNetLetter = IntNetLetter +1
WScript.Echo "UNC Path " & intDrive & " - " & colDrives.Item(intDrive)_
& " Mapped drive No : " & colDrives.Item(intDrive +1)
Next

Wscript.Quit(1)

' Guy's Script ends here

Learning Points

Note 1: The If... then else section Lines 14 -18 is not strictly necessary, however I included this code to alert people who do not have any mapped network drives.

Note 2: This method has two separate functions.  EnumNetworkDrives itself (Line 12), which makes the mapped network drives 'visible'.  Later, colDrives.Count (Line 21-25) is able to read the mapped network drives because they have been enumerated.

Note 3: To Guy the most puzzling part of the script is .count -1.  My latest theory is that this command tells the script to walk through the list of mapped network drives starting with the lowest number.  As ever, practical tests are the best, if try .count 1 or any positive number, it does not work, if you try -3 it skips the first mapped drive pair.

Note 3a: Extra Explanation by Bob Monahon

This means:
- For: Start with intDrive = 0
- Step: At each subsequent iteration of the loop, increase intDrive by 2
- To: Stop after intDrive is equal to or greater than (colDrives.Count-1) .. that is, the next-to-last entry in the list.

In plain words, this loop processes the colDrives collection in groups of 2 items. The first iteration processes items 0 and 1; the next iteration "steps up by 2", to process items 2 and 3; and so forth. In the last iteration, it processes the next to last entry and the last entry; and exits.

Note 4: Step 2 is the best value.  Step with any odd number produces a subscript out of range error, while step 4, takes giant paces and misses every other drive.

Note 5: I have taken up Emre's suggestion to use the variable colDrives rather than objDrives.  Emre's reasoning is that it refers to a collection of objects, hence 'col' rather than 'obj'.

Brandon Finton Writes With a Further Explanation of this Loop:

In response to learning point 3 about the .count-3:

Bob's explanation is only half right.

The loop will start at 0 (intDrive = 0) and will stop at (colDrives.Count-1) and will increment (or step) by 2.  So, we'll start at 0, do our work, add 2, do our work, add 2 more, etc, etc, until intDrive and (.count-1) are the same values.  We use .Count-1 because .count will return the complete number of values (say 12), but since we are starting our loop at 0 (because colDrives.Item() is a zero based array with 0 being the first value, 1 being the second, etc, etc) we need to stop at 11 (.count-1) if we want to loop 12 times.  If we left out the -1 the loop would run 13 times and we'd get an "index is outside the bounds of the array" error because we'd be trying to retrieve the 13th record from an array that is only 12 records long.

So, Bob was correct about the incrementing part but was not quite right about "stopping on the next to last entry"

Lastly, in response to note 4, regarding the step value, 2 is used because the items within the colDrives.Items() array returns the drive letter ("Z:") and then the share name (\\Some-Server\Some-Share)

Even values (including 0) returns the drive letter, odd values returns the path

You get the error, as expected, if you try and disconnect "\\Some-Server\Some-Share". This can be tested by removing/commenting the Step and then displaying the item:

For intDrive = 0 to (colDrives.Count – 1)
MsgBox(colDrives.Item(intDrive))
Next

EnumNetworkDrives Summary

EnumNetworkDrives is a tricky technique.  The best approach is to first master the MapNetworkDrive method.  An added bonus of starting with MapNetworkDrive is that you will have an actual drive to enumerate.  Pay careful attention to the syntax; from the spelling with the plural drives, to understanding the two parts, enumerating the drives, then displaying the array of drive letter and UNC path.

Once you have mastered EnumNetworkDrives, then you can play 'If.. then.. else' games with your logon scripts. For example, if drive M: is already mapped, then remove it and then map a different UNC path.  See more here.


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

See Also

Logon Script Home  MapNetworkDrive  RemoveNetworkDrive  Already Connected

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's Network Performance Monitor (NPM)

Orion NPM is designed for detecting network outages.

Network-centric views (screenshot) make it easy to see what's working, and what needs your attention.

Download your free trial of Orion's network performance monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.