All,
I've got a script I'm trying to use to remap drives from one server to
another (contingency plan in case the first server has problems). It
works great... except it seems to skip a drive on my machine. The code
is posted below. When I look in the registry at \HKCU\Network\ I have 4
drives: J, K, Y, Z. However, my script does not appear to look at the J
drive. I used WScript.Echo to verify which drives it is analyzing.
(BTW, I apologize in advance for the poorly written code! Doing two
separate tests [one on name and one on IP] was simply easier than
learning how to do it properly heh]). Thanks in advance!
Option Explicit
Dim objNetwork, objDrive, intDrive, strOldShare,_
strOldIP, strNewShare, strNewUNC, strDriveLetter, objFSO
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objDrive = objNetwork.EnumNetworkDrives
Set objFSO = CreateObject("Scripting.FileSystemObject")
strOldShare = "server1"
strOldIP = "123.123.123.123"
strNewShare = "server2"
'If we have no network drives, quit
If objDrive.Count = 0 Then
Wscript.Quit(0)
End If
For intDrive = 0 to objDrive.Count - 1 Step 2
WScript.Echo "Doing drive " & objDrive.Item(intDrive)
WScript.Echo LCase(objDrive.Item(intDrive + 1))
If InStr (1,LCase(objDrive.Item(intDrive + 1)), strOldShare, 1) > 0
Then
strNewUNC = Replace(LCase(objDrive.Item(intDrive + 1)),
strOldShare, strNewShare, 1, 1, 1)
strDriveLetter = objDrive.Item(intDrive)
WScript.Echo "For " & strDriveLetter & " I will replace " &_
objDrive.Item(intDrive + 1) & " with " & strNewUNC
' Only delete and recreate if the destination exists
If objFSO.FolderExists(strNewUNC) Then
objNetwork.RemoveNetworkDrive strDriveLetter, True, True
objNetwork.MapNetworkDrive strDriveLetter, strNewUNC, True
Else
WScript.Echo "The mapped network drive " & strDriveLetter & " at
" &_
objDrive.Item(intDrive) & " was not mapped to " & strNewUNC &_
".The destination did not exist."
End If
End If
If InStr (1,LCase(objDrive.Item(intDrive + 1)), strOldIP, 1) > 0 Then
strNewUNC = Replace(LCase(objDrive.Item(intDrive + 1)), strOldIP,
strNewShare, 1, 1, 1)
strDriveLetter = objDrive.Item(intDrive)
WScript.Echo "For " & strDriveLetter & " I will replace " &_
objDrive.Item(intDrive + 1) & " with " & strNewUNC
' Only delete and recreate if the destination exists
If objFSO.FolderExists(strNewUNC) Then
objNetwork.RemoveNetworkDrive strDriveLetter, True, True
objNetwork.MapNetworkDrive strDriveLetter, strNewUNC, True
Else
WScript.Echo "The mapped network drive " & strDriveLetter & " at
" &_
objDrive.Item(intDrive) & " was not mapped to " & strNewUNC &_
".The destination did not exist."
End If
End If
Next
WScript.Quit(1)