Use VBS to find all the drives mapped to the share and remap them


Hello, script expert! How do I find all the initiators mapped to \ \ Server1 / share and remap them to \ \ server2 / share?

— H T-S

Hi, h.t.s. You know, not long ago, Malcolm Gladwell published a book called the tipping point. In short, the book makes a hypothesis: something may be ignored for a long time, but when the neglect at least reaches the so-called tipping point, the thing that no one cares about will suddenly become a real fashion. It’s like overnight it’s going from something you’ve never heard of to something you can see everywhere.

This is an interesting assumption, and we seem to see this in the scripts used to map and unmap network drives. We post “Hello, script expert!” It’s been more than a year since the column, and no one seems to pay attention to it. Then, all of a sudden, there are a lot of questions about mapping and unmapping network drives. We answered the first of these questions a few weeks ago, and now we have another one. We have an in box full of other questions about network drives. First the hula hoops, then the bell bottoms, and now it’s the network driver’s turn. Think for yourself.

How about remapping the network drive? Well, for better or worse, there is no way to automatically remap network drives; so we have to go back and find other solutions. But it’s not too bad: we can find all the drives that match the criteria, unmap them, and then remap each drive to a new location.  

Of course, it sounds complicated, but it’s actually very simple. The following script can be used to find all the drives mapped to \ \ Server1 / share and remap them to \ \ server2 / share:

Set objNetwork = CreateObject(“Wscript.Network”)

Set colDrives = objNetwork.EnumNetworkDrives

For i = 0 to colDrives.Count-1 Step 2
    If colDrives.Item(i + 1) = “\\server1\share” Then
        strDriveLetter = colDrives.Item(i)
        objNetwork.RemoveNetworkDrive strDriveLetter
        objNetwork.MapNetworkDrive strDriveLetter, “\\server2\share”
    End If

The script will be created first Wscript.Network Object instance. It should be noted that whenever we want to map or unmap a network drive, we need to use Windows Script Host because WMI has no way to map or unmap a drive. It doesn’t matter, it just means that our script has to run on the local computer. In general, the WSH method cannot be used for remote computers. This is a limitation that you have to face. (there is a way to solve this problem: run the script as a login script. The login script will always run locally.)

After creating the network object, call the enumnetworkdrives method to return a collection of all mapped network drives on the computer:

Set colDrives = objNetwork.EnumNetworkDrives

This will allow us to see for ourselves that strange little thing called a set of mapped network drives. Today, we won’t go into details about the architecture of this collection. Please refer to the previous column on network drives. Just state that each mapped drive actually occupies two items in this collection: the first is the drive letter and the second is the “UNC” path. If there are three mapped drives on the computer, the contents of the collection will look like this:


That’s why we have to use this strange looking “for next” loop to traverse the collection. This code uses us to skip every other item in the collection to ensure that we only look at each drive letter item:

For i = 0 to colDrives.Count-1 Step 2

Then, for each drive letter, we need to determine whether the corresponding “UNC” path is \ \ Server1 / share1. Remember, if you look at the ﹣ 0 ﹣ item in the collection (the index number of the first item in the collection is ﹣ 0), you will see the drive letter, and the corresponding ﹣ UNC ﹣ path will add ﹣ 1 to the index number (0). Therefore, we use the following code to determine whether the first drive is exactly mapped to \ \ Server1 / share1:

If colDrives.Item(i + 1) = “\\server1\share” Then

Let’s assume that’s it. In this case, we need to get the drive letter (item 0) and store the value in a variable called strdriveletter. Then, call the mapnetworkdrive method to unmap the drive, and then call the mapnetworkdrive method to remap the same drive letter to the new share

objNetwork.MapNetworkDrive strDriveLetter, “\\server2\share”

No, it’s not the so-called “tipping point,” because you’ve just overturned the idea of trying to follow it all. We know it’s a bit confusing, but this is due to the special construction of the set of mapped network drives. If it doesn’t make any sense to you, just ignore it, and you should see that things are logical as a whole. Maybe, it’s a bit of confusing logic, but it’s still logic.

Because this is more difficult to understand, let’s take the simplest example: remap the share named \ \ Server1 / share to the share named \ \ server2 / share. Of course, you can remap any share on ﹣ Server1 ﹣ to any share with similar name on ﹣ server2 ﹣ as well. But it may be a bit too much for today. However, if you are interested, just let us know that we will pay more attention to this topic in the near future.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]