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.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

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

  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 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 MonitorSolarwinds Network Device 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

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

®

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

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 in Your 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 EnumNetworkDrives. 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 displaying network drives:

# PowerShell Logon 3 Script Example
Clear-Host
$PSnet = $(New-Object -ComObject WScript.Network)
$PSnet.objNetwork.EnumNetworkDrives
$PSnet

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 a PowerShell logon script.

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

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

 *


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: