Script to change users manually mapped drives to a new server with new server name
-
01-07-2021 - |
Question
I have several hundred users whom each have manually mapped drives to a variety of locations/ folders on a file server.
I am migrating the data to a new server with a new name but keeping the same folder structure so require to script the remapping of their drives, I could do this with a simple script if they followed a uniform pattern of mappings but as I say they are all unique manual mapping.
So I need to interrogate their current mapping and change the server name from \server1.ourdomain\whatever path to \server2.ourdomain\whatever path etc as I say the paths will mostly be to a variety of folders and levels as the structure is quite deep and complex.
I intend to use a VB Script and run it as a GPO on the users Site.
Solution
Try something like this:
oldserver = "\\server1.ourdomain"
newserver = "\\server2.ourdomain"
Set net = CreateObject("WScript.Network")
Set drives = net.EnumNetworkDrives
For i = drives.Count - 1 To 0 Step -2
If LCase(Left(drives(i), Len(oldserver))) = oldserver Then
net.RemoveNetworkDrive drives(i-1), True, True
net.MapNetworkDrive drives(i-1), Replace(drives(i), oldserver, newserver), True
End If
Next
Edit: Since all of your examples had .ourdomain attached to them, I was assuming that you were always using FQDNs. If NetBIOS names are being used as well, the above script won't work for them of course. However, you can't shorten just oldserver
to \\server1
, because then the instruction
Replace(drives(i), oldserver, newserver)
would change a UNC path \\server1.ourdomain\share
into \\server2.ourdomain.ourdomain\share
, which obviously won't work. Either remove the domain part from both oldserver
and newserver
, or (if you want to enforce FQDNs) use something like this:
domain = ".ourdomain"
oldserver = "\\server1"
newserver = "\\server2" & domain
Set net = CreateObject("WScript.Network")
Set drives = net.EnumNetworkDrives
For i = drives.Count - 1 To 0 Step -2
net.RemoveNetworkDrive drives(i-1), True, True
If InStr(1, drives(i), domain, vbTextCompare) > 0 Then
newpath = Replace(drives(i), oldserver & domain, newserver)
Else
newpath = Replace(drives(i), oldserver, newserver)
End If
net.MapNetworkDrive drives(i-1), newpath, True
Next
OTHER TIPS
there is a slight issue with the above script (not really the script more the place its being applied) being:
IF the user has mapped the drive via the short name for example \server1 as opposed to FQDN \server1.ourdomain then the script will not work, it simply ignores it.
If I change the script to use the shortname as the "old name" variable then it works for drives mapped with short name but errors with th3e FQDN and unmaps the FQDN drive unable to remap it.
I have got around this by remapping FQDN first and then the short names as they wont error with long names as the server name changed already, I’m sure there is a more eloquent way to do this but I simply reapplied the script with the short on the second pass as below :
oldserver = "\\server1.ourdomain"
newserver = "\\server2.ourdomain"
Set net = CreateObject("WScript.Network")
Set drives = net.EnumNetworkDrives
For i = drives.Count - 1 To 0 Step -2
If LCase(Left(drives(i), Len(oldserver))) = oldserver Then
net.RemoveNetworkDrive drives(i-1), True, True
net.MapNetworkDrive drives(i-1), Replace(drives(i), oldserver, newserver), True
End If
Next
oldserver = "\\Server1"
newserver = "\\server2.ourdomain"
Set net = CreateObject("WScript.Network")
Set drives = net.EnumNetworkDrives
For i = drives.Count - 1 To 0 Step -2
If LCase(Left(drives(i), Len(oldserver))) = oldserver Then
net.RemoveNetworkDrive drives(i-1), True, True
net.MapNetworkDrive drives(i-1), Replace(drives(i), oldserver, newserver), True
End If Next
Works fine with 1 exception , in the users windows explorer or my computer when the user has multiple drives mapped to the \server1 it only reflects the change to 1 drive, but it has remapped all of the drives correctly as using the “net use” command in shows the mapping to have changed for them all ?
if they log off and on it reflects correctly by showing in explorer all of the drives correctly with new name, so it seems to be a weird explorer display issue .........very strange and any ideas ?
If you try yourself you will see as I had a few people test this by mapping one drive to short and one to long and it updates both but only displays one change ?