Scripting School: Creating an inventory of mapped drive connections

Many smaller enterprises have trouble keeping track of user connections to mapped drives. If users are creating and deleting their own network connections, it can be easy for them to duplicate or create redundant mappings, or to forget which drive letter corresponds to which network path. If users have a lot of mappings, one way to avoid this problem is to have a list printed out in front of them.

Happy New Year! During the Christmas holidays I went back and looked at what my column has covered since early

2005. Since I've had many requests via email to go back to basics, this column will examine how to further apply some of the concepts that we've already introduced.

Many smaller enterprises have a problem when it comes to keeping track of user connections to mapped drives. If users are creating and deleting their own network connections, it can be easy for them to duplicate or create redundant mappings, or to forget which drive letter corresponds to which network path. If users have a lot of mappings, one way to avoid this problem is to have a list printed out in front of them. Such a list can also help the Help Desk (for when a user calls up trying to remember which drive letter they should save a file to).

To create this list, we're going to combine two concepts we've explored previously: the Wscript.Network object and the File System Object (FSO) for writing output to a text file. To accomplish this task, this script must:

  • Get the network connections on the local computer
  • Organize the network connections
  • Label them with the source computer's name and the name of the logged-in user
  • Save this file to the appropriate location

Forgot the basics of Wscript.Network? We covered the fundamentals of Wscript.Network back in June 2005, but the basic idea is that you need to create an object to work with and then assign that object a name to make it easier to work with. We also covered the basics of writing information to a text file in November 2005, but it's the same principle: Create an object of the right type so that you can access its methods.

Get the network connections

Wscript.Network organizes the references to the local computer, currently logged-in user and the computer's domain. We've already discussed the methods of connecting to a printer and setting the default printer. This time, we're going to use a different method: EnumNetworkDrives.

Set oNet= WScript.CreateObject("WScript.Network")
Set oDrives = oNet.EnumNetworkDrives
oUser = oNet.UserName
oComputer = oNet.ComputerName
'Wscript.Echo oUser
WScript.Echo "Network drive mappings for "&oUser& " on " & oComputer
For i = 0 to oDrives.Count - 1 Step 2
WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
Next

What we've done here is return an array of all the drive letter connections, correlating the mapped drive letter with the UNC path that it corresponds to, and then labeling it with the name of the currently logged-in user and the computer they're using. As you may recall, the ampersands are the glue that sticks together output that's a combination of strings and variables. (This information is helpful if more than one person uses the same computer, or if a person uses more than one computer.) The output looks like this example:

Network drive mappings for Christa on IMP:
Drive Z: = Impscripts

To get a specific mapping, you'd refer to it by its index number, beginning with 0. In this case, we want the whole list, but in another scenario you could use that capability to find all mappings to a particular computer and loop through it to check for duplicates.

Creating the output

So far, so good. But as soon as I close that command window, the output is gone. To make this script really useful, I want to save the output as a file so I can refer to it later. To do that, I'll be referring to the FSO like this:

Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFilesys.CreateTextFile("h:drive mappings for "&oUser&" on "&oComputer&".txt", True)
sPath = oFilesys.GetAbsolutePathName("h:drive mappings for "&oUser&" on "&oComputer&".txt")
sFilename = oFilesys.GetFileName(sPath)
oFiletxt.WriteLine("This file holds inventory of some kind.")
oFiletxt.Close'
If oFilesys.FileExists(sPath) Then Wscript.Echo "The list of mappings stored in",sFilename&"."

(For the sake of argument, assume that we're using the oUser and oComputer objects already created. This snippet will not work independently.)

The variable names in the file names allow us to label the files according to which user is using which computer. Any time the script is run, it will overwrite the previous file, so you'll have a current record, and it will be easy to tell at a glance which inventory applies to a particular user/computer combination. (If you want to keep historical information about the drives, you can add the Now variable to the file name as you've added the user name and computer name. This will timestamp the file so that the previous versions won't get overwritten.)

The WriteLine method here isn't currently doing anything useful, just writing a string of data. What we want it to do is copy the output from our enumeration of the drive mappings. Simple enough: we'll replace the Wscript.Echo commands with WriteLine. The whole thing looks like this:

Set oNet= WScript.CreateObject("WScript.Network")
Set oDrives = oNet.EnumNetworkDrives
oUser = oNet.UserName
oComputer = oNet.ComputerName
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFilesys.CreateTextFile("c:drive mappings for "&oUser&" on "&oComputer&".txt", True)
sPath = oFilesys.GetAbsolutePathName("c:drive mappings for "&oUser&" on "&oComputer&".txt")
sFilename = oFilesys.GetFileName(sPath)
oFiletxt.WriteLine "Network drive mappings for "&oUser& " on " & oComputer
For i = 0 to oDrives.Count - 1 Step 2
oFiletxt.WriteLine "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
Next
oFiletxt.Close
If oFilesys.FileExists(sPath) Then Wscript.Echo "The list of mappings is stored in",sFilename&"."

In this column, we've looked at another aspect of the Wscript.Network object: its ability to enumerate mapped drives on a computer. Using this object's computer name and user name properties, we've labeled the array of mapped drives. Then, using the File System Object, we've created a text file and dumped the output, and saved the file. Save this file to a network location, and the Help Desk can help your users figure out which path corresponds to which drive letter—and can help your users keep from duplicating their self-managed mappings.

ABOUT THE AUTHOR:
Christa Anderson
Christa Anderson, a Windows Server and Terminal Services MVP, is the Regional Director, North America, for visionapp, a company that automates the deployment of terminal servers and MetaFrame servers and preconfigured applications. When Christa began working with Windows Server operating systems in 1992, she became increasingly interested in finding more efficient and flexible ways of performing routine tasks. Since then she has written extensively about administrative scripting and has taught technical sessions on the subject at various conferences, helping people who had never done any scripting to write their own scripts in half a day.

A former Program Manager for the Microsoft Terminal Services team, she is an internationally known authority on server-based computing. She is the author of Windows Terminal Services, The Definitive Guide to MetaFrameXP, and co-author of the book Mastering Windows 2003 Server. To send a question about scripting to Christa, e-mail her at editor@searchwincomputing.com.

Dig deeper on Windows Disaster Recovery and Business Continuity

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close