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

Source: Internet
Author: User
Ask:
Hello, Scripting Guy! How do I find all the initiators that map to \\server1\share and remap them to \\server2\share?

--H-T

For:
Hello, H-T-S. You know, not long ago Malcolm Gladwell published a book titled "The Tipping Point." In short, the book makes the assumption that something may be overlooked for a long time, but when this omission reaches at least the so-called tipping point, the original thing will suddenly become a veritable fashion. It's like overnight it seems to be everywhere from something you've never heard of.

This is an interesting assumption, and we seem to see this on the script for mapping and canceling mapped network drives. We release "Hello, Scripting Guys!" "It's been more than a year since the column has never been mentioned, and no one seems to be paying attention to the problem." Then, all of a sudden, questions about mapping and canceling mapped network drives are pouring in. We answered the first question a couple of weeks ago, and now we're going to answer another question, and we have an inbox full of other questions about network drives. First the hula hoop, then the horn pants, and now it's the network drive. Go ahead and think for yourself.

So what about remapping a network drive? Well, for better or worse, there is no way to automatically remap a network drive; Therefore, we have to go back and find other solutions. But that's not too bad: we can find all the drives that match the criteria, unmapped the drives, and remap each drive to the new location.

Of course, that sounds complicated, but it's actually very simple. The following script can be used to find all 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
Next

The script will first create the Wscript.Network object instance. We should note that whenever we want to map or cancel a mapped network drive, we need to use Windows Script Host, because WMI does not have any methods for mapping or canceling the mapped drive. It doesn't matter, it just means that our script must be running on the local computer. The WSH method cannot normally be used against a remote computer. This is a limitation that you have to face. (There is a way to fix this problem: run the script as a logon script.) The logon script will always run locally. )

After you create 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 with our own eyes the strange little Thing called a collection of mapped network drives. Today we are not going to detail the architecture of this collection, see the previous column on network drives. Suffice to say 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 you have three mapped drives on your computer, the contents of the collection will look like the following:

X:
\\server1\share1
Y:
\\server2\share2
Z:
\\server3\share3

That's why we have to iterate through the collection with a seemingly bizarre for Next loop, which exercises us to skip an item every other item in the collection, making sure that we see only the individual drive letter entries:

For i = 0 to coldrives.count-1 Step 2

Then, for each drive letter, we need to determine if the appropriate UNC path is \\server1\share1. Keep in mind that if you view the 0 items in the collection (the index number of the first item in the collection is 0), you see the drive letter, and the corresponding UNC path will be the index number (0) plus 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's it. In this case, we need to get the drive letter (0 entries) and store the value in a variable named Strdriveletter. Then, call the RemoveNetworkDrive method to cancel the mapping of 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 a "tipping point" because you've just overturned the idea of trying to follow all this. We know it's a bit confusing, but this is due to the way the mapped network drive collection is specially constructed. If this doesn't make any sense to you, just ignore it, and you should see that things are logical on the whole. Maybe, it's a little confusing logic, but it's still logic.

Because this is a bit confusing, we give the simplest example of remapping a share named \\server1\share to a share named \\server2\share. You can, of course, remap any share on the Server1 to any name-like share on the server2. But it may be a bit too much for today. However, if you are interested, just let us know that we will be looking at this topic again in the near future.

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: info-contact@alibabacloud.com 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.