Use vbs to map all the drives to the shared disk and remap them.

Source: Internet
Author: User

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 titled the tipping point. Simply put, the book assumes that a thing may be ignored for a long period of time, but when such ignorance at least reaches the so-called tipping point, this old unattended thing will suddenly become a real fashion. It seems like it becomes everywhere from a thing you have never heard.

This is an interesting assumption. We seem to see this phenomenon in the script for ing and un ing the network drive. We released "Hello, script expert !" The column has been around for more than a year and has never mentioned network drives, and it seems that no one has noticed this problem. Then, all of a sudden, questions about the ing and un ing of network drives come together. We answered the first such question a few weeks ago, and now we have another question. One of our inbox is all about the network drive. First, hula hoop, then Bell pants. Now it's the turn of the network drive. Think about it yourself.

How can I remap the network drive? Well, whether it is good or bad, there is no way to automatically remap the network drive; therefore, we have to go back and find other solutions. But this is not too bad: We can find all qualified drives, unmap these drives, and remap each drive to a new location.

Of course, this sounds complicated, but it is actually very simple. The following script can be used to find all the drives mapped to \ server1 \ share and remap these drives 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

This script will first create a wscript. Network object instance. We should note that Windows Script Host is required whenever we need to map or unmap the network drive, because WMI does not have any method to map or unmap the drive. It doesn't matter, it just means that our script must run on the local computer. Generally, wsh cannot be used for remote computers. This is a limit you have to face. (There is one way to solve this problem: Run this script as a login script. The logon script will always run locally .)

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

Set coldrives = objnetwork. enumnetworkdrives

This will let us see with our own eyes the strange little thing called the ing network drive set. Today, we will not detail the architecture of this set. Please refer to the previous column about network drives. You only need to note that each mapped drive actually occupies two items in this set: the first item is the drive letter, and the second item is the UNC path. If there are three mapped drives on the computer, the set content will be as follows:

\ Server1 \ share1
\ Server2 \ share2
\ Server3 \ servers 3

This is the reason why we must use such a strange for next loop to traverse the set.CodeExercise that we skip one item at a time in the set to ensure that we only view 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 view 0 items in the Set (index number of the first item in the set is 0), you will see the drive letter, and the corresponding UNC path will be the index number (0) add 1. Therefore, we use the following code to determine whether the first drive is mapped to \ server1 \ share1:

If coldrives. Item (I + 1) = "\ server1 \ share" then

Let's assume that this is the case. In this case, we need to get the drive letter (item 0) and store the value in the variable strdriveletter. Then, call the removenetworkdrive method to cancel the ing to the drive, and then call the mapnetworkdrive method to remap the same drive letter to the new share:

Objnetwork. mapnetworkdrive strdriveletter, "\ server2 \ share"

No, this is not the so-called "tipping point", because you have just overturned the idea of trying to follow it all. We know it is a bit confusing, but this is caused by the special constructor that has mapped the network drive set. If this doesn't make sense to you, you should see that things are logical as a whole despite ignoring it. Maybe it is a bit confusing logic, but it is still logic.

This is confusing, so we give the simplest example: remap the share named \ server1 \ share to share named \ server2 \ share. Of course, you can also remap any share on server1 to any share with similar names on server2. But this may be a little too much for today. However, if you are interested, you only need to let us know that we will pay attention to this topic soon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.