Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to change users manually mapped drives to a new server with new server name

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.

like image 882
Iain Binnie Avatar asked Dec 18 '25 15:12

Iain Binnie


1 Answers

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
like image 176
Ansgar Wiechers Avatar answered Dec 21 '25 11:12

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!