script to change existing drive mappings to point to new servername

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
LVL 9
MCPJoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
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
MCPJoeAuthor Commented:
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
vinnyd79Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

MCPJoeAuthor Commented:
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
vinnyd79Commented:
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
MCPJoeAuthor Commented:
Ok, I'll give that a shot.  Will that map the drive as persistent?  
0
vinnyd79Commented:
ooops,forgot that. Change this line:

WshNetwork.MapNetworkDrive drv, NewShare

To:

WshNetwork.MapNetworkDrive drv, NewShare , True
0
MCPJoeAuthor Commented:
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
vinnyd79Commented:
That means the script found a share on the old server that does not exist on the new server.
0
MCPJoeAuthor Commented:
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
vinnyd79Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MCPJoeAuthor Commented:
Thats ok, I want this to be transparent, because end users may be running this script.  Thanks for your help!!! It works!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.