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
 

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'.

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

Webcomputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

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

Please report a broken link, or an error.