尝试这样的事情:
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
编辑:由于您的所有示例都附加了 .ourdomain,因此我假设您一直在使用 FQDN。如果还使用了 NetBIOS 名称,那么上面的脚本当然对它们不起作用。但是,你不能只缩短oldserver
到\\server1
,因为那样的话指令
Replace(drives(i), oldserver, newserver)
会将 UNC 路径更改\\server1.ourdomain\share
为\\server2.ourdomain.ourdomain\share
,这显然是行不通的。oldserver
要么从and中删除域部分newserver
,要么(如果要强制执行 FQDN)使用如下内容:
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