Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

script to change existing drive mappings to point to new servername

Posted on 2006-04-12
12
Medium Priority
?
400 Views
Last Modified: 2010-05-01
Can anyone help me with this?  I'm trying to find a way to script the modification of existing Windows 2000/XP drive mappings through vbscript.  I am migrating file servers and want to find a way to simply email a script to my users that will change their existing drive mappings to point to the new file server.  I know there must be a way to do this.  Any help is appreciated.  

Basically, want to change existing drive mapping (\\servera\sharename) to a new server name with the same sharename (\\serverb\sharename).  

Thanks
0
Comment
Question by:MCPJoe
  • 6
  • 6
12 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 16435862
Here is a simple example that will check to see if drive S: is mapped. If it is,it will remove it and remap to the specified share:

Dim WshNetwork,fso
Set WshNetwork = WScript.CreateObject("WScript.Network")

Set fso = CreateObject("Scripting.FileSystemObject")
if fso.DriveExists("S") Then
      WshNetwork.RemoveNetworkDrive "S:", true, true
end if

WshNetwork.MapNetworkDrive "S:", "\\serverb\sharename"
Set fso = Nothing
Set WshNetwork = Nothing
0
 
LVL 9

Author Comment

by:MCPJoe
ID: 16435916
My users will likely have at least two drive mappings with various drive letters, (drive letters won't be the same from PC to PC).  Is there an easy way to check for a drive mapping, then change the servername path if it matches the old server name?  Then map the drive with the new servername and make it persistent?

Thanks
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 16436002
Something like this?


Dim WshNetwork,drv,DriveList,x
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set DriveList = WshNetwork.EnumNetworkDrives

For x = 1 to DriveList.Count Step 2
   if Lcase(DriveList.Item(x)) = Lcase("\\servera\share") Then
        drv = DriveList.Item(x -1)
      WshNetwork.RemoveNetworkDrive drv, true, true
                WshNetwork.MapNetworkDrive drv, "\\serverb\sharename"
   end if
Next

Set WshNetwork = Nothing
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:MCPJoe
ID: 16436037
So that will search through the existing drive mappings, check if a particular server is used, then delete the drive mappins and re-create them with the new servername?

Thanks
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 16436178
Actually that is checking for the full share name. I think you want something like this:


Dim WshNetwork,drv,DriveList,x,NewShare
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set DriveList = WshNetwork.EnumNetworkDrives

For x = 1 to DriveList.Count Step 2
   if Instr(Lcase(DriveList.Item(x)),Lcase("\\servera\")) > 0 Then
        NewShare = Replace(DriveList.Item(x),"\\servera\","\\serverb\")
      drv = DriveList.Item(x -1)
        WshNetwork.RemoveNetworkDrive drv, true, true
        WshNetwork.MapNetworkDrive drv, NewShare
   end if
Next

Set WshNetwork = Nothing
0
 
LVL 9

Author Comment

by:MCPJoe
ID: 16436197
Ok, I'll give that a shot.  Will that map the drive as persistent?  
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 16436301
ooops,forgot that. Change this line:

WshNetwork.MapNetworkDrive drv, NewShare

To:

WshNetwork.MapNetworkDrive drv, NewShare , True
0
 
LVL 9

Author Comment

by:MCPJoe
ID: 16439395
When I run your script, I get

Line 10
Char 9
Error: the network name cannot be found
code: 80070043
Source: WSHNetwork.MapNetworkDrive

How can I fix that?

Thanks
Joe
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 16439430
That means the script found a share on the old server that does not exist on the new server.
0
 
LVL 9

Author Comment

by:MCPJoe
ID: 16439440
Oh ok.  Sorry, my mistake.  Thanks again!!!!  It worked then, I was wondering why it was doing one drive, but not the other.  I appreciate the help!!!

thanks
Joe
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 2000 total points
ID: 16439465
You can verify this by adding a messagebox in to show the value of the NewShare string. If this is the case,let me know how you want to handle this and I will modify the script accordingly.


Dim WshNetwork,drv,DriveList,x,NewShare
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set DriveList = WshNetwork.EnumNetworkDrives

For x = 1 to DriveList.Count Step 2
   if Instr(Lcase(DriveList.Item(x)),Lcase("\\servera\")) > 0 Then
        NewShare = Replace(DriveList.Item(x),"\\servera\","\\serverb\")
        drv = DriveList.Item(x -1)
       
      MsgBox NewShare   ' add messagebox to see what NewShare Value is

      WshNetwork.RemoveNetworkDrive drv, true, true
               WshNetwork.MapNetworkDrive drv, NewShare,True
   end if
Next

Set WshNetwork = Nothing
0
 
LVL 9

Author Comment

by:MCPJoe
ID: 16439964
Thats ok, I want this to be transparent, because end users may be running this script.  Thanks for your help!!! It works!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question