Link to home
Start Free TrialLog in
Avatar of MCPJoe
MCPJoeFlag for United States of America

asked on

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
Avatar of vinnyd79
vinnyd79

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
Avatar of MCPJoe

ASKER

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
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
Avatar of MCPJoe

ASKER

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
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
Avatar of MCPJoe

ASKER

Ok, I'll give that a shot.  Will that map the drive as persistent?  
ooops,forgot that. Change this line:

WshNetwork.MapNetworkDrive drv, NewShare

To:

WshNetwork.MapNetworkDrive drv, NewShare , True
Avatar of MCPJoe

ASKER

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
That means the script found a share on the old server that does not exist on the new server.
Avatar of MCPJoe

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MCPJoe

ASKER

Thats ok, I want this to be transparent, because end users may be running this script.  Thanks for your help!!! It works!!!