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 Syntax
- Example EnumNetworkDrives
- PowerShell 3.0 Logon Scripts
♦
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.
Guy Recommends: WMI Monitor and It’s Free!
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.
Download your free copy of WMI Monitor
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
- On Line 10, change the server name from ‘\\alan’ to your server name.
- 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
- Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
- Change the server name from "\\alan to the name of your server.
- Save the file with .vbs extension e.g. Already.vbs.
- 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 https://computerperformance.co.uk/
‘ Version 1.3 – April 24th 2010
‘ —————————————————-‘
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’ 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:
Download a free trial of NPM (Network Performance Monitor)
Here is the main EnumNetworkDrives Script
Instructions to EnumNetworkDrives
- Copy and paste the script below into notepad.
- Save the file with .vbs extension e.g. EnumNetworkDrives.vbs.
- 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 https://computerperformance.co.uk/
‘ Version 1.8 – April 24th 2010
‘ ———————————————————–‘
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.
®