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
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:
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.
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try - it's free.
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
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.
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.
Instructions to MapNetworkDrive
' AddDrive.vbs - ' Windows Logon Script
Guy Recommends SolarWinds' Free Network Monitor
Thus utility makes it easy to check the health of a router or firewall. Check the real-time performance, and availability statistics, for any device on your network. Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl - it's free. Download your free Network Device Monitor
If you need more comprehensive
network analysis software:
Here is the main EnumNetworkDrives Script
Instructions to EnumNetworkDrives
' EnumNetworkDrives.vbs - Windows Logon Script
' This is the heart of the script
' Extra section to troubleshoot
' Here is the where the script reads the array
' Guy's Script ends here
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.